我如何防止每个实体集的延迟加载 [英] how do i prevent lazyloading per entityset basis

查看:69
本文介绍了我如何防止每个实体集的延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这让我在EF4中遇到了麻烦,所以我想知道是否有更简单的方法来解决这个问题。想象一下我有这个。


var customer =  ...


customer.Orders.Attach(customer.Orders.CreateSourceQuery(o => ; o.ShipCity ==" London"));


我正在部分加载订单集合。但是,如果在上下文级别没有将lazloading设置为false,那么当我使用customer.Orders时,将拉出整个对象图。我不知道为什么我必须完全禁用延迟加载来告诉
EF这个集合已加载或阻止延迟加载发生。 DbContext上是否有一个选项来告诉该集合已加载。我知道在v1中,EntityCollection具有IsLoaded属性但它永远不可设置。现在我们有ICollection,
IsLoaded甚至不是一个选项。 


这里有任何选项吗?


Zeeshan Hirani 实体框架4.0食谱由Apress


http://weblogs.asp.net/zeeshanhirani

解决方案

< span style ="font-size:small"> Zeeshan,


< p style ="margin:0in 0in 0pt; background:white">  


IsLoaded在DbContext更改跟踪API上可用。例如,您可以检查IsLoaded,如
所以:


 


&NBSP;&NBSP;&NBSP;
var = isLoaded = context.Entry(customer).Collection(a => a.Orders).IsLoaded;


 


  ; 但是,它目前无法设置,因为您无法设置基础核心EF库中加载的
导航属性—正如您在帖子中指出的那样。 &NBSP;
一旦我们更新了核心EF库,我们就完全打算实现setter,这样你就能做到这一点:$ b​​ $ b


 


   
context.Entry(customer).Collection(a => a.Orders).IsLoaded =
true
;


 


谢谢,


Arthur


Now this has bugged me in EF4 so i am wondering if there would be an easier way to solve this. imagine i have this.

var customer =  ...

customer.Orders.Attach(customer.Orders.CreateSourceQuery(o => o.ShipCity == "London"));

I am partially loading the orders collection. However if do not set lazloading to false at the context level, when i foreach over customer.Orders, the entire object graph would be pulled. I don't know why i have to completely disable lazyloading to tell EF that this collection is loaded or prevent lazy loading from occurring. Is there an option on the DbContext to tell that this collection is loaded. I know in v1, EntityCollection had IsLoaded property but it was never settable. Now that we have ICollection, IsLoaded is not even an option. 

Any options here?


Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
http://weblogs.asp.net/zeeshanhirani

解决方案

Zeeshan,

 

IsLoaded is available on the DbContext change tracking API. For example, you can check IsLoaded like so:

 

    var = isLoaded = context.Entry(customer).Collection(a => a.Orders).IsLoaded;

 

 However, it is currently not settable because it is not possible to set a nav property as loaded in the underlying core EF libraries—as you point out in your post.  Once we update the core EF libraries we fully intend to implement the setter such that you will be able to do this:

 

    context.Entry(customer).Collection(a => a.Orders).IsLoaded = true;

 

Thanks,

Arthur


这篇关于我如何防止每个实体集的延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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