如何深度复制的实体实体、深度

2023-09-07 13:43:09 作者:是个废人

我发现这个片段here:

 公共静态牛逼DeepClone< T>(这件T OBJ)
    {
        使用(VAR毫秒=新的MemoryStream()){
            变种BF =新的BinaryFormatter();
            bf.Serialize(MS,OBJ);
            ms.Position = 0;
            返程(T)bf.Deserialize(MS);
        }
    }
 

这表示,我们可以通过这件事情做的所有相关对象的深层副本。

我试图做一个副本是这样的:

  db.Detach(myEntity所);
myEntity.EntityKEy = NULL;
实体newEntity =新实体();
newEntity = DeepClone<实体>(实体);
db.Entities.AddObject(newEntity);
db.SaveChanges();
 

它的工作原理,但仍然不会复制任何嵌套\相关记录。我该怎么办错在这里?

我有这样的结构实体 - > ChildEntity - > ChildChildEntity - > - 一对许多 所以我想,当我复制的实体也将复制所有子记录。

更新: 后建议,我这样做:

 实体newEntity =新实体();
Eneity实体= db.Include(ChildEntity)在哪里。(P => p.Id ==编号)。单();
newEntity = DeepClone<实体>(实体);
db.Detach(myEntity所);
myEntity.EntityKEy = NULL;
db.Entities.AddObject(newEntity);
db.SaveChanges();
 
基于深度学习的实体关系联合抽取研究综述

在ADDOBJECT行获取异常:

  

具有相同键的对象已经存在于ObjectStateManager。   该ObjectStateManager无法跟踪具有相同的多个对象   关键的。

解决方案

最重要的一点是,你必须加载相关实体,创造深克隆分离之前,。如果分离的实体中的所有关系都默默地删除,因为分离方法仅适用于单一的实体和实体图形不能既包括安装和拆卸的实体。这是一个原因,你需要的只是调用序列化,而不是分离

不要忘记关闭延迟加载,否则你的序列化将拉动数据从数据库中其他的导航性能以及。还记得这个深层副本将在图中创建的所有实体的新版本,以便添加根实体将增加所有相关实体。

I found this snippet here:

public static T DeepClone<T>(this T obj)
    {
        using (var ms = new MemoryStream()) {
            var bf = new BinaryFormatter();
            bf.Serialize(ms, obj);
            ms.Position = 0;
            return (T)bf.Deserialize(ms);
        }
    }

Which says that we can do deep copy of all related objects through this thing.

I'm trying to do a copy like this:

db.Detach(myEntity); 
myEntity.EntityKEy = null;
Entity newEntity = new Entity();
newEntity = DeepClone<Entity>(Entity);
db.Entities.AddObject(newEntity);
db.SaveChanges();

IT works, but still does not copy any nested\related records. what do I do wrong here?

I have this structure Entity->ChildEntity ->ChildChildEntity -> - one-to-many so I assume when I copy entity it will also copy all child records.

UPDATE: After suggestions, I did this:

Entity newEntity = new Entity();
Eneity Entity = db.Include("ChildEntity").Where(p=>p.Id==Id).Single();
newEntity = DeepClone<Entity>(Entity);
db.Detach(myEntity); 
myEntity.EntityKEy = null;
db.Entities.AddObject(newEntity);
db.SaveChanges();

Getting exception on AddObject line:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

解决方案

The important point is that you must load related entities and create deep clone prior to detaching. If you detach the entity all relations are silently removed because Detach method works only for single entity and entity graph cannot consists of both attached and detached entities. That is a reason why you need serialization instead of simply calling Detach.

Don't forget to turn off lazy loading otherwise your serialization will pull data for other navigation properties from database as well. Also remember that this deep copy will create new version of all entities in the graph so adding the root entity will add all related entities as well.