一个图可以从ObjectContext中分离出来并重新附加到另一个? [英] Can a graph be detached from an ObjectContext and be re-attached to another one?

查看:84
本文介绍了一个图可以从ObjectContext中分离出来并重新附加到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试以下方式分离实体对象的图形,然后将其附加到新的上下文中:

I tried the following to detach a graph of entity objects, and then attach it to a new context:

// create a context
var ctx = new TestEntities();
var parents = ctx.Parents;

// populate the graph
var newParent = new Parent {Nb = 1, Title = "Parent1"};
parents.AddObject(newParent);
newParent.Children.Add(new Child {Nb = 1, Title = "Child1"});

// put all entity objects in Unchanged state before detaching
ctx.SaveChanges(); 

// detach all entity objects
foreach (var objectStateEntry in ctx.ObjectStateManager.GetObjectStateEntries(~EntityState.Detached))
    ctx.Detach(objectStateEntry.Entity);

// create a new context
ctx = new TestEntities(); 

// attach graphs to new context
foreach (var p in parents)
    ctx.Attach(p);

我有两个这样的问题:


  1. 在分离所有实体对象后, newParent.Children 变为空

  2. 重新生成InvalidOperationException - 附加说实体对象不能被IEntityChangeTracker的多个实例引用。

  1. After detaching all entity objects, newParent.Children becomes empty
  2. An InvalidOperationException is raised when re-attaching saying that "An entity object cannot be referenced by multiple instances of IEntityChangeTracker".

有谁知道如何正确分离图从一个ObjectContext,并重新附加到另一个?

Does anyone know how to properly detach a graph from an ObjectContext, and re-attach it to another one?

更新:

对我来说好消息,我想出了如何在同一个ObjectContext中更改底层的数据库连接,所以我不需要再分离/附加。如果有人有兴趣,这里是我如何做(这里我使用SQLite并更改数据库文件):

Ok good news for me, I figured out how to change the underlying database connection within the same ObjectContext, so I don't need to detach/attach anymore. If anybody's interested, here's how I do it (here I use SQLite and change the database file):

var sc = ((EntityConnection)ctx.Connection).StoreConnection;
sc.ConnectionString = @"Data Source=" + newFile + ";";

我会接受Ladislav的答案,因为它似乎是正确的,并回答我的问题,因为它被问。

I'll accept Ladislav's answer as it seems to be correct and answers my question as it was asked.

推荐答案

您必须创建整个图形的深层克隆,并将其附加到另一个上下文。深层克隆是通过序列化创建的。通常的方法是使用 DataContractSerializer

You must create deep clone of the whole graph and attach it to another context. The deep clone is created through the serialization. The common approach is to use DataContractSerializer:

var serializer = new DataContractSerializer(typeof(Parent));
serializer.WriteObject(stream, attachedEntity);
stream.Seek(0, SeekOrgin.Begin);
detachedEntity = (Parent)serializer.ReadObject(stream);

为了使这项工作,您的实体不能包含循环引用(父级有导航属性到Child and Child has导航属性到父级),或者您必须使用实体上的 DataContract(IsReference = true) DataMember 属性来通知序列化它必须跟踪引用以解决循环引用问题。

To make this work your entities must not contain circular references (Parent has navigation property to Child and Child has navigation property to Parent) or you must use DataContract(IsReference=true) and DataMember attributes on your entities to inform serializer that it must track references to resolve circular reference issue.

这篇关于一个图可以从ObjectContext中分离出来并重新附加到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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