简体   繁体   English

如何使用Entity Framework关联“fixup”?

[英]How do I use Entity Framework association “fixup”?

I need to retrieve a large hierarchy of entities from my database, with many relationships. 我需要从我的数据库中检索一个具有许多关系的大型实体层次结构。 Rather than create one giant queries with many Includes , I've read that it's possible to use a number of smaller queries to fetch different parts of the hierarchy, then EF somehow glues everything together using something called "association fixup". 我没有创建一个包含许多Includes巨型查询,而是读到可以使用一些较小的查询来获取层次结构的不同部分,然后EF以某种方式使用称为“关联修正”的东西将所有内容粘合在一起。 However I'm struggling to get it to work. 但是我很难让它发挥作用。 (I'm using EF5 and POCO by the way). (顺便说一句,我正在使用EF5和POCO)。

As a simple example, I'm trying to retrieve a Customer and all their related Orders using the "fixup" technique. 举个简单的例子,我正在尝试使用“fixup”技术检索客户及其所有相关订单。 This is what I'm doing in my BLL tier:- 这就是我在我的BLL层中所做的事情: -

var customer = context.Customers
    .Where(o => o.Id == requestedCustomerId).SingleOrDefault();

customer.Orders = context.Orders
    .Where(o => o.CustomerId = requestedCustomerId).ToList();

When I examine the customer entity returned to the UI tier, customer.Orders gives an ObjectDisposedException. 当我检查返回到UI层的客户实体时,customer.Orders给出了ObjectDisposedException。 What am I doing wrong? 我究竟做错了什么?

As a further example of how fixup would be used, how would I populate the Orders' related OrderLines (either in a separate query, or as part of the second query above)? 作为如何使用fixup的另一个例子,我如何填充Orders相关的OrderLines(在单独的查询中,或作为上面第二个查询的一部分)? And what if the OrderLine entity had a ProductCategory parent entity - how would I populate these? 如果OrderLine实体有一个ProductCategory父实体怎么办 - 我将如何填充这些实体?

Update 更新

I've just tried the following, which works:- 我刚试过以下工作:

var orders = context.Orders
    .Where(o => o.CustomerId = requestedCustomerId).ToList();

var customer = context.Customers
    .Where(o => o.Id == requestedCustomerId)
    .Include("Orders")
    .SingleOrDefault();

Am I right in saying that the second query won't fetch Orders from the database again, and EF will associate those retrieved by the previous query (even though they are just sitting in some arbitrary variable)? 我是否正确地说第二个查询不会再次从数据库中获取订单,EF将关联前一个查询检索到的订单(即使它们只是坐在某个任意变量中)?

The ObjectDisposed exception is probably because you have lazy loading turned on and you are disposing your context before trying to access customer.Orders . ObjectDisposed异常可能是因为您启用了延迟加载,并且在尝试访问customer.Orders之前处理了上下文。 I don't like lazy loading, so I always turn it off: 我不喜欢延迟加载,所以我总是把它关掉:

// in your context constructor:
this.Configuration.LazyLoadingEnabled = false;

Your original example should now work. 您的原始示例现在应该可行。


In your update, you are eagerly loading Customer.Orders with the Include clause. 在您的更新中,您急切地使用Include子句加载Customer.Orders This means EF is getting this data from the database using a join when it gets the Customer record. 这意味着EF在获取Customer记录时使用join从数据库获取此数据。


EDIT: 编辑:

A DbContext "remembers" every object it has seen, where an object is basically a database row. DbContext “记住”它所见过的每个对象,其中一个对象基本上是一个数据库行。

If you load the Orders associated with a particular Customer then the context remembers those Order objects. 如果加载与特定Customer关联的Orders ,则上下文会记住这些Order对象。

When you subsequently fetch the Customer object ( without .Include( "Orders" ) ), EF is smart enough to attach the Order objects you previously fetched to the Customer object. 当您随后获取Customer对象(没有.Include( "Orders" ) )时,EF足够智能,可以将先前提取的Order对象附加到Customer对象。

It can do this, because the Order objects have a CustomerId , so when you get that Customer object it can look at its CustomerId property and add the Order objects to the Customer.Orders collection. 它可以这样做,因为Order对象具有CustomerId ,因此当您获得Customer对象时,它可以查看其CustomerId属性并将Order对象添加到Customer.Orders集合。

It will only attach the Orders it knows about, so if you haven't previously loaded all the associated Order records for some reason, EF will just attach those it has seen. 它只会附加它所知道的Orders ,因此如果您之前由于某种原因没有加载所有相关的Order记录,EF将只附加它看到的那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM