Entity Framework 4 延迟加载实体集
我正在使用带有 POCO 对象的实体框架,并且我已经打开了 LazyLoading。
如果我正在使用与对象关联的集合,该集合何时完全加载以及在什么情况下加载?
如果我调用其中的每一个,
Order.OrderItems.Count()
Order.OrderItems.Any(x => x.StatusId = aValue)
Order.OrderItems.All(x => x.StatusId = aValue)
其中任何一个都可以保证 OrderItems 集合的完整加载吗? 调用
Order.Include(“OrderItems”)
在代码中,我们在查询时或
context.LoadProperty(order, “OrderItems”)
查询后
但我意识到有时这种情况并不总是发生 - 我想知道这种情况不发生的后果。我认为我对此有一点知识差距
I'm working with entity framework with POCO objects and I've got LazyLoading turned on.
If i'm working with a collection associated with an object, when is the collection fully loaded and in what circumstances?
If I call each of these
Order.OrderItems.Count()
Order.OrderItems.Any(x => x.StatusId = aValue)
Order.OrderItems.All(x => x.StatusId = aValue)
Do any of these guarantee the complete loading of the OrderItems collection?
At points in the code we are calling
Order.Include(“OrderItems”)
when querying or
context.LoadProperty(order, “OrderItems”)
after querying
But I’ve realised sometimes this isn’t always happening – and I want to know the consequences of this not occurring. I think I’ve got a bit of a knowledge gap with it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Linq 方法通常不会加载数据,直到您对其结果进行
foreach
或附加ToList
、ToArray
等。这就是方法Linq to Entities 提供程序可以允许您链接方法并且仅在最终结构上构建查询。如果方法返回
IQueryable
或IEnumerable
,您可以非常确定 Linq 此时不会加载数据。但是:
对于最外层查询,如果 Linq 必须返回布尔值或整数,那么它必须立即运行您的查询。但对于嵌套查询,事情会变得更加复杂,因为在评估外部查询之前不会评估它们。
至于:
我相信
Include
不会自行解析查询。当您进行另一个查询时,您可以使用它来承载附加数据。Linq 概述
如果您想了解(一般)Linq 的工作原理,可以查看这组文章:
http://msmvps.com/blogs/jon_skeet/archive/tags/Edulinq/default.aspx
这将使您了解何时可以使用
IEnumerable
推迟,以及何时必须评估。它不会告诉您有关 Linq to Entities 提供程序的所有信息,但很大一部分知识将会转移。Linq methods don't generally load your data until you either
foreach
over their result, or append aToList
,ToArray
, etc. This is how the Linq to Entities provider can allow you to chain methods and only build a query over the final structure.If a method returns an
IQueryable
orIEnumerable
, you can be pretty sure that Linq won't load the data at that point.But:
For an outer-most query, if Linq must return a boolean value or an integer then it must run your query immediately. But things get a little more complicated for nested queries, since they won't be evaluated until the outer query gets evaluated.
As for:
I believe an
Include
won't resolve a query on its own. You use it to piggyback additional data when you're making another query.Linq in general
If you want to know (generally) how Linq works, you can check out this set of articles:
http://msmvps.com/blogs/jon_skeet/archive/tags/Edulinq/default.aspx
That will give you an idea of when an
IEnumerable
can be deferred, and when it must be evaluated. It won't tell you everything about the Linq to Entities provider, but a good portion of the knowledge will transfer.是查询。
当您调用
.ToList()
(或其他一些迭代器将其具体化为对象)时,将执行这些查询。如果您使用.Include
,那么当您执行查询时,包含的属性将被填充(查询将包含该表的联接)。使用Any
或All
(或任何过滤器扩展,如Where
)只会添加 where 条件(可以来自另一个表),但它不会将 OrderItems 放入 sql select 中,因此除非您使用.Include
,否则它们不会被加载。--
与您的问题无关,但如果您对此很熟悉,并且有分页功能,请记住在调用
ToList 之前将
(执行查询),我见过人们很早就调用Skip
和Take
添加到您的查询中ToList
并在进行分页之前将整个表读取到内存中。are queries.
These queries are executed when you call
.ToList()
(or some other iterator to materialize it into objects). If you use.Include
then included property will be populated (query will contain join for that table) when you execute query. UsingAny
orAll
(or any filter extension likeWhere
) only adds where condition (which can be from another table), but it does not put OrderItems into sql select, so they will not be loaded unless you use.Include
.--
Not related to your question, but if you are fresh with this, and you have paging, remember to add
Skip
andTake
to your query before callingToList
(executing query), I have seen people callingToList
very early and reading whole table to memory before doing paging.