克隆实体框架上的数据 [英] Cloning data on Entity Framework

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

问题描述



现在我需要使用Entity Framework进行复制/克隆操作。首先我开始这样写:

 
foreach(table1中的sourcedata1)
{
...创建新表
...复制数据
...创建Guid
...添加
foreach(table2中的sourcedata2)
{
...创建新的表
...复制数据
...创建Guid
...添加

...等等
}
}

问题是这不是一个很好的方法。有没有任何简单的方法克隆信息(除了Guid需要为新行生成)或者我应该手动复制一切吗?



其他解决方案



您还可以使用EmitMapper或AutoMapper来复制属性。

解决方案

使用直接序列化,您可以这样做:



http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/a967b44b-c85c-4afd- a499-f6ff604e2139 /



使用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:

http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/a967b44b-c85c-4afd-a499-f6ff604e2139/

Using Reflection, but with lots more code you can do this: http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx

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

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