实体框架 - 麻烦与.Load() [英] Entity Framework - Trouble with .Load()

查看:109
本文介绍了实体框架 - 麻烦与.Load()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注这篇文章, http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5 -part-6-loading-related-entities.aspx

I have been following this article, http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

具体来说,标题为在显式加载相关实体时应用过滤器一节。

Specifically the section titled "Applying filters when explicitly loading related entities".

我需要执行以下操作:

db.Configuration.LazyLoadingEnabled = false;
var class = db.Classes.Find(1);
db.Entry(class).Collection(c => c.Students).Query().Where(s => s.grade > 2.0).Load();

当我深入分析并观察SQL分析器时,我看到加载该类的查询。然后我看到应该加载学生的查询,但是class.Students从不填充,并保持为空。但是,如果我从SQL分析器复制学生查询并自行运行,则返回相应的学生。看来,Entity Framework正在运行学生查询并获得正确的结果,但是并没有将它们附加到类对象。

When I step through this and watch SQL profiler I see the query that loads the class. Then I see the query that should load the Students, but class.Students is never populated and remains null. However if I copy the students query from SQL profiler and run in myself, the appropriate students are returned. It seems that Entity Framework is running the students query and getting to proper results back, but is not attaching them to the class object.

有几种方法可以解决这个问题,但是我想知道我是否错过了一个步骤,或者如果我没有正确使用.Load()。

There are ways I can work around this, but I wondering if I missed a step, or if I am not using .Load() properly.

推荐答案

如果 Class 学生是一种多对多关系,您所看到的行为是预期的(虽然令人困惑,我承认)。首先,如果您阅读了Intellisense关于 Load 方法的说明...

If the relationship between Class and Student is a many-to-many relationship the behaviour you are seeing is expected (although confusing, I'd admit). First of all, if you read what Intellisense says about the Load method...


枚举查询,以便对于诸如
System.Data.Entity.DbSet,
System.Data.Objects.ObjectSet,
System.Data.Objects.ObjectQuery的服务器查询,和其他人的
查询的结果将被加载到相关的System.Data.Entity.DbContext,
System.Data.Objects.ObjectContext或客户端上的其他缓存
。这个
相当于调用ToList,然后抛出列表
,而没有实际创建列表的开销。

Enumerates the query such that for server queries such as those of System.Data.Entity.DbSet, System.Data.Objects.ObjectSet, System.Data.Objects.ObjectQuery, and others the results of the query will be loaded into the associated System.Data.Entity.DbContext, System.Data.Objects.ObjectContext or other cache on the client. This is equivalent to calling ToList and then throwing away the list without the overhead of actually creating the list.

...它不表示您运行查询的实体的导航集合被填充,只有结果被加载到上下文中。

...it doesn't say that the navigation collection of the entity you run the query with gets populated, only that the result is loaded into the context.

当您调用 Load 时,导航集合(一对多关系)将不会被填充,这并不是这种方法的结果,而是后续的上下文处理,称为关系跨度或修复,这是一种不涉及多对多关系的处理。

That the navigation collection in case of a one-to-many relationship is populated when you call Load is not really the result of this method but of a subsequent processing of the context called relationship span or fix-up, a processing that does not take place for many-to-many relationships.

在这个问题和答案中有更多的细节: EF 4.1加载过滤的儿童集合不适用于多对多

In this question and answer are more details: EF 4.1 loading filtered child collections not working for many-to-many

精髓在于 - 对于一个ma ny-to-many关系 - 您必须使用 ToList()而不是 Load()直接填充导航集合:

The quintessence is that - for a many-to-many relationship - you must populate the navigation collection directly by using ToList() instead of Load():

var class1 = db.Classes.Find(1);
class1.Students = db.Entry(class1).Collection(c => c.Students).Query()
    .Where(s => s.grade > 2.0).ToList();

这将加载学生进入上下文填充导航集合 class1 同时。

This will load the students into the context and populate the navigation collection in class1 at the same time.

这篇关于实体框架 - 麻烦与.Load()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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