问题描述
我有形成树形关系的实体。
I have entities which form a tree relationships.
class MyEntity
{
public int Id {get;set;}
public int ParentId {get;set;}
public virtual MyEntity Parent {get;set;}
public virtual ICollection<MyEntity> Children {get;set;}
}
在这些实体被称为没有 AsNoTracking()
关系是固定的了。
When these entities are called without AsNoTracking()
relationships are fixed up.
var entities = MyEntitiesSet.ToList();
所有的导航属性和集合设置
但是,如果 AsNoTracking()
被称为:
var entities = MyEntitiesSet.AsNoTracking.ToList();
没有导航属性设置。这是可以理解的。但我不明白为什么收集和naviagtion属性不会重写提供关系修正了这个code:
no navigation property is set. This is understandable. But I cannot understand why collection and naviagtion properties are not overriden to provide relationship fixup for this code:
entity.Parent = anotherEntity;
在这里,我想到的是 anotherEntity.Children
集现在包含实体。唉,这是错误的期望,我experimetns显示。
Here I expect that anotherEntity.Children
collection now contains entity. Alas, this is false expectation as my experimetns show.
时有可能得到所需的行为不启用更改跟踪?
Is it possible to get desired behavior without enabling change tracking?
更新1
我loked在生成的代理,并指出,被覆盖的集合是代理类型的hashsets。 TEntity&GT; 什么是真实的的。
I loked at generated proxies and noted that overriden collections are hashsets of the proxy type. They are not backed up by EntityCollection<TEntity>
what was true to EF 4 ObjectContext proxies.
和我已经找到了答案此处。
的DbContext不会产生这些修复了关系代理。
And I've found the answer here.
DbContext does not generate proxies which fix up relationships.
推荐答案
的DbContext的不生成它修复了关系代理。
DbContext does not generate proxies which fix up relationships.
证明是在这里:http://connect.microsoft.com/VisualStudio/feedback/details/760609/poco-navigation-proxies-should-fixup-the-other-end-automatically
然而,当entitties被附加到上下文加载相关实体:
Yet, when entitties are attached to the context loading related entities:
context.ASet.ToList();
context.BSet.ToList();
自动设置的关系。
sets relationships automatically.
这篇关于是否有可能使关系修正时更改跟踪被禁止,但生成代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!