Entity Framework 4 延迟加载实体集

发布于 2024-12-15 03:01:42 字数 525 浏览 1 评论 0原文

我正在使用带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷了相思 2024-12-22 03:01:42

Linq 方法通常不会加载数据,直到您对其结果进行 foreach 或附加 ToListToArray 等。这就是方法Linq to Entities 提供程序可以允许您链接方法并且仅在最终结构上构建查询。

如果方法返回 IQueryableIEnumerable,您可以非常确定 Linq 此时不会加载数据。

但是:

Order.OrderItems.Count()
Order.OrderItems.Any(x => x.StatusId = aValue)
Order.OrderItems.All(x => x.StatusId = aValue)

对于最外层查询,如果 Linq 必须返回布尔值或整数,那么它必须立即运行您的查询。但对于嵌套查询,事情会变得更加复杂,因为在评估外部查询之前不会评估它们。

至于:

Order.Include("OrderItems")

我相信 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 a ToList, 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 or IEnumerable, you can be pretty sure that Linq won't load the data at that point.

But:

Order.OrderItems.Count()
Order.OrderItems.Any(x => x.StatusId = aValue)
Order.OrderItems.All(x => x.StatusId = aValue)

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:

Order.Include("OrderItems")

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.

别把无礼当个性 2024-12-22 03:01:42
Order.OrderItems.Any(x => x.StatusId = aValue)
Order.OrderItems.All(x => x.StatusId = aValue)

是查询。

当您调用 .ToList() (或其他一些迭代器将其具体化为对象)时,将执行这些查询。如果您使用 .Include ,那么当您执行查询时,包含的属性将被填充(查询将包含该表的联接)。使用 AnyAll (或任何过滤器扩展,如 Where)只会添加 where 条件(可以来自另一个表),但它不会将 OrderItems 放入 sql select 中,因此除非您使用 .Include,否则它们不会被加载。

--

与您的问题无关,但如果您对此很熟悉,并且有分页功能,请记住在调用 ToList 之前将 SkipTake 添加到您的查询中(执行查询),我见过人们很早就调用ToList并在进行分页之前将整个表读取到内存中。

Order.OrderItems.Any(x => x.StatusId = aValue)
Order.OrderItems.All(x => x.StatusId = aValue)

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. Using Any or All (or any filter extension like Where) 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 and Take to your query before calling ToList (executing query), I have seen people calling ToList very early and reading whole table to memory before doing paging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文