克隆实体框架上的数据 [英] Cloning data on Entity Framework
问题描述
现在我需要使用Entity Framework进行复制/克隆操作。首先我开始这样写:
foreach(table1中的sourcedata1)
{
...创建新表
...复制数据
...创建Guid
...添加
foreach(table2中的sourcedata2)
{
...创建新的表
...复制数据
...创建Guid
...添加
...等等
}
}
问题是这不是一个很好的方法。有没有任何简单的方法克隆信息(除了Guid需要为新行生成)或者我应该手动复制一切吗?
其他解决方案
您还可以使用EmitMapper或AutoMapper来复制属性。
使用直接序列化,您可以这样做:
使用Reflection,但可以使用更多的代码:
http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner .aspx
I am creating software where user can create new product based on older product.
Now I need to make copying / cloning operations with Entity Framework. First I started writing like this:
foreach(sourcedata1 in table1) { ... create new table ... copy data ... create Guid ... add foreach(sourcedata2 in table2) { ... create new table ... copy data ... create Guid ... add ... and so on } }
Problem is that this not a nice way to do it. Is there any easy way clone information (except Guid that needs to be generated for new rows) or should I manually copy everything?
Other solution
You could also use EmitMapper or AutoMapper to do copying of the properties.
Using straight serialization, you can do this:
Using Reflection, but with lots more code you can do this: http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx
这篇关于克隆实体框架上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!