本文介绍了将新实体与现有实体相关联时无意的延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用启用了延迟加载的POCO实体。我注意到的一件事是,当我将一个新的分离实体与现有实体关联时,我会对数据库进行不必要的查询。在下面的代码中,我正在尝试将地址添加到与用户关联的地址集合
。设置地址实体的用户导航属性的行会导致查询数据库,加载当前与用户关联的所有地址:

I am using POCO entities with lazy loading enabled. One thing I noticed is that when I associate a new detached entity with an existing one I get unwanted query to the database. In the code below, I'm trying to add an address to the collection of addresses associated with a user. The line that sets the User navigation property of the address entity causes a query to the database, loading all addresses that are currently associated with the user:

 


var user = (from u in context.Users
   where u.Name == "John Doe"
		 select u).Single();
var address = context.CreateObject<Address>();
address.Street = "1234 Broadway";
address.City = "San Francisco"
// The following line causes a query that retrieves all addresses associated with the user
address.User = user;
context.SaveChanges();

推荐答案

欢迎到EF论坛!

由于启用了延迟加载,我认为很难实现您的关联修复功能也已启用。  基于ADO.NET团队博客,

http://blogs.msdn.com/b/efdesign/archive/2010/03/10/poco-template-code-generation-options.aspx

As the Lazy Loading is enabled, I think it's really hard to realize your request with association fixup also enabled.   Based on ADO.NET team blog,http://blogs.msdn.com/b/efdesign/archive/2010/03/10/poco-template-code-generation-options.aspx:

为实体上的每个导航属性编写一个fixup方法,只要其值发生变化,就会从导航属性的setter调用。
其目的是确保双向关系的每一端与另一端保持同步。
例如,在Cutomer和Order之间的一对多关系中,每当设置Order.Customer时,fixup方法都会确保订单位于Customer’ s Orders集合中。它还保留相应的外键属性即。 Order.CustomerID
与新客户的主键(ID)值同步。如果POCO实体独立于EF堆栈使用,则此逻辑非常有用,例如编写针对不会打击数据库的测试。 Fixup确保对象图
的连接方式与使用EF时的方式相同。修复方法有点复杂,因此如果您计划在EF独立方案中使用实体,则自动生成它们很有用。

A fixup method is written for every navigation property on an entity and is called from the setter of the navigation property whenever its value changes.Its purpose is to ensure that each end of a bidirectional relationship stays in sync with the other.For example, in a one-to-many relationship between Cutomer and Order, whenever Order.Customer is set, the fixup method ensures that the Order is in the Customer’s Orders collection. It also keeps the corresponding foreign key property viz. Order.CustomerID in sync with the new Customer’s primary key (ID) value. This logic can be useful if the POCO entities are used independently of the EF stack, like for writing tests against them which don’t hit the database. Fixup ensures that the object graph is connected in the same way as you would expect while using them with EF. Fixup methods are a bit complex to write and hence it is useful to have them auto-generated if you are planning on using the entities in an EF independent scenario.

这可能会导致一些意外问题如果我们在POCO实体上禁用关联修正。 有关解决方法,如何直接设置外键列? 

It may cause some unexpected issues if we disable the association fixup on POCO entities.  For a workaround, how about directly setting the foreign key column? 

     address.UserID = user.UserID;

     address.UserID = user.UserID;

 

如果您有任何疑问,请随时与我们联系。

Please feel free to let me know if you have any questions.

美好的一天!


这篇关于将新实体与现有实体相关联时无意的延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:29