实体框架多目标上下文 [英] Entity Framework Multiple Object Contexts

查看:267
本文介绍了实体框架多目标上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被问500不同的时间在50个不同的方式......但在这里要再次重申,因为我似乎无法找到我要找的答案:

This question has been asked 500 different times in 50 different ways...but here it is again, since I can't seem to find the answer I'm looking for:

我使用与POCO代理EF4。

I am using EF4 with POCO proxies.

一个。 我有对象的时候,我从一个ObjectContext中的一个实例获取。这ObjectContext的配置。

A. I have a graph of objects I fetched from one instance of an ObjectContext. That ObjectContext is disposed.

乙。 我有一个对象,我从ObjectContext中的另一个实例获取。这ObjectContext的也被释放。

B. I have an object I fetched from another instance of an ObjectContext. That ObjectContext has also been disposed.

我想从B A使用单位设置了一堆东西相关的属性....像

I want to set a related property on a bunch of things from A using the entity in B....something like

foreach(var itemFromA in collectionFromA)
{
   itemFromA.RelatedProperty = itemFromB;
}

当我这样做,我得到的异常:

When I do that, I get the exception:

System.InvalidOperationException occurred
  Message=The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedTarget, Boolean applyConstraints, Boolean addRelationshipAsUnchanged, Boolean relationshipAlreadyExists, Boolean allowModifyingOtherEndOfRelationship, Boolean forceForeignKeyChanges)
       at System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedEntity, Boolean applyConstraints)
       at System.Data.Objects.DataClasses.EntityReference`1.set_ReferenceValue(IEntityWrapper value)
       at System.Data.Objects.DataClasses.EntityReference`1.set_Value(TEntity value)
       at 

我想我需要从ObjectContexts时,他们才能处理的上述工作分离这些实体......问题是,拆下所有的实体从我的ObjectContext时,其配置似乎破坏图形。如果我是这样的:

I guess I need to detach these entities from the ObjectContexts when they dispose in order for the above to work... The problem is, detaching all entities from my ObjectContext when it disposes seems to destroy the graph. If I do something like:

objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged)  
.Select(i => i.Entity).OfType<IEntityWithChangeTracker>().ToList()  
.ForEach(i => objectContext.Detach(i));

所有图中的关系似乎得到未固化

All the relations in the graph seem to get unset.

我如何去解决这个问题呢?

How can I go about solving this problem?

推荐答案

@Danny Varod是正确的。您应该使用一个的ObjectContext 整个工作流程。此外,因为您的工作流程似乎一个逻辑功能包含多个窗口,它应该也可以使用单一的presenter。然后,你将遵循推荐的方法:每presenter单一的背景下。您可以拨打的SaveChanges 多次,所以它不应该打破你的逻辑。

@Danny Varod is right. You should use one ObjectContext for the whole workflow. Moreover because your workflow seems as one logical feature containing multiple windows it should probably also use single presenter. Then you would follow recommended approach: single context per presenter. You can call SaveChanges multiple times so it should not break your logic.

这个问题的根源是众所周知的问题,与动态代理的不足。这些代理仍然持有引用的背景下,当你处理它。正因为如此,他们认为他们仍然附接到上下文和它们不能被连接到另一上下文。如何迫使他们释放参考上下文的唯一方法是手动拆卸。在同一时间,一旦你从分离的背景下它是从相关的附加实体删除,因为你不能有装拆的实体组合在同一张图中的实体。

The source of this issue is well known problem with deficiency of dynamic proxies generated on top of POCO entities combined with Fixup methods generated by POCO T4 template. These proxies still hold reference to the context when you dispose it. Because of that they think that they are still attached to the context and they can't be attached to another context. The only way how to force them to release the reference to the context is manual detaching. In the same time once you detach an entity from the context it is removed from related attached entities because you can't have mix of attached and detached entities in the same graph.

这个问题其实并不在code调用occures:

The issue actually not occures in the code you call:

itemFromA.RelatedProperty = itemFromB;

但在相反的操作以修正方法触发:

but in the reverse operation triggered by Fixup method:

itemFromB.RelatedAs.Add(itemFromA);

我觉得要解决这个办法是:

I think the ways to solve this are:

  • 请不要做到这一点,使用工作整机单个上下文 - 这是应该使用
  • 删除反向导航属性,以便修正方法不会触发code。
  • 请不要使用POCO T4模板修正方法或修改T4模板不生成它们。
  • 关闭延迟加载和代理的创建进行这些操作。这将消除动态代理从波苏斯正因为如此,他们将独立于上下文。

要关闭代理的创建和延迟加载使用:

To turn off proxy creation and lazy loading use:

var context = new MyContext();
context.ContextOptions.ProxyCreationEnabled = false;

您可以真正尝试编写自定义的方法来分离整个对象图,但正如你所说的有人问500次,我还没有看到工作的解决方案还没有 - 除了序列化和反序列化的新对象图

You can actually try to write custom method to detach the whole object graph but as you said it was asked 500 times and I haven't seen working solution yet - except the serialization and deserialization to the new object graph.

这篇关于实体框架多目标上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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