自动化机是否防止用EF延迟加载? [英] Is automapper preventing lazy loading with EF?

查看:97
本文介绍了自动化机是否防止用EF延迟加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用AutoMapper,它似乎得到我所有的子实体(即使我没有在Include()子句中指定它们)。有没有办法让懒惰加载可能,只有在我指定的时候才能获得子属性。

I have been using an AutoMapper and it seems that it gets me all the child entities (even if I don't specify them in "Include()" clause). Is there any way how to make lazy loading possible and get child properties only if I specify them.

谢谢,

Jakub

推荐答案

映射后,您将具有映射对象,而不引用源实体(其拥有延迟加载的数据库上下文)。只有属性值被复制到目标实体。所以你不能在没有源代码实体的情况下执行任何延迟加载。

After mapping you will have mapped object without any references to source entity (which holds database context for lazy loading). Only property values are copied to destination entity. So you will not be able to do any lazy-loading without source entity.

实际上,懒惰的加载工作对你而言是正常的 - 它发生在映射过程中。您为实体的惰性加载属性指定了映射,并且映射程序尝试获取这些值。这导致延迟加载您为映射配置的所有导航属性。这是非常低效的。要在映射期间禁用延迟加载,您可以忽略映射配置中的导航属性。例如。如果您有客户订购懒惰订单:

Actually lazy loading works just fine for you - and it's occur during mapping process. You specified mappings for lazy-loaded properties of your entity, and mapper tries to get those values. That results in lazy-loading all navigation properties which you have configured for mapping. This is very inefficient. To disable lazy-loading during mapping you can ignore navigation properties in mapping configuration. E.g. if you have customer with lazy-loaded orders:

Mapper.CreateMap<Customer, CustomerDto>()
      .ForMember(s => s.Orders, m => m.Ignore());

或从目的地实体删除订单 CustomerDto 。如果您需要具有内部订单的 CustomerDto 实例,那么最好的选择是尽快加载订单,以避免其他查询。

Or remove Orders property from your destination entity CustomerDto. If you need to have CustomerDto instance with orders inside, then best option is to do eager loading of orders, to avoid additional queries.

这篇关于自动化机是否防止用EF延迟加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆