我有以下LINQ to SQL对象(例如)
class Parent{
int id; // primary key
IEnumerable<Child> children;
}
class Child{
int id; // primary key
string field1;
int field2;
}
我需要深度克隆一个 Parent
并将其保存到数据库中,但是使用子节点的COPIES,即不引用现有的子节点 .
我已经使用this method来做克隆,但我正在寻找 iterating through the parent and children properties 的优雅方式(假设可能有大量子对象,级联远远超过1级)和 setting their primary keys to 0 以便当我将克隆对象提交给在数据库中,LINQ to SQL负责创建新的子项 .
1 回答
您可以尝试使用
System.Reflection
的以下扩展方法: