将实体与所有映射实体一起克隆 [英] Cloning an entity together with all mapped entities

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

问题描述

我需要一种可靠的方法来克隆现有实体,以及通过导航属性映射到它的所有实体。



例如,如果我有一个带有运送和账单地址的实体订单,一个订单位置列表,每个都有附加状态(等等),我想对实体及其附加的所有内容进行深层复制,并将其作为新数据行一次性保存在相应的表中。我可以通过分离实体,修改它们然后将它们保存到数据库来欺骗实体框架这样做,但我不确定这是个好主意。



我不是在寻找黑客,它更多的是不能很好地了解数据模式以避免错误,并编写可以与模式中未来变化相关的万无一失的代码。



编辑:如图所示,我根本不需要复制实体。显然,克隆单个实体所需要的只是再次添加它。现在问题是映射实体会发生什么。它们是否也会被克隆,是否调整了Ids和外键?



我尝试过:



目前我分别复制和存储每个实体。如果我得到了正确的数据模式,那么现在可以使用,但是要打破数据模式中的未来变化。

I need a reliable way to clone an existing entity, along with all entities that are mapped to it via navigation properties.

For example, if I have an entity 'Order' with shipping and billing addresses, a list of order positions, each with a status attached (and so on), I would like to make a deep copy of the entity and everything attached to it and save it in the database as new data rows in the respective tables in one go. I could trick the entity framework into doing this by detaching the entities, modifying them and then saving them to the database, but I'm not sure that this is a good idea.

I'm not looking for a hack, it's more about not knowing the data schema well enough to avoid mistakes right now and writing foolproof code that can live with future changes in the schema.

As it appears, I do not have to copy the entities at all. Apparently all it takes to clone a single entity, is to add it again. Now the question is what happens to the mapped entities. Will they also be cloned and the Ids and foreign keys adjusted or not?

What I have tried:

Presently I copy and store each entity separately. Provided that I got the data schema right, this would work for now, but break with future changes in the data schema.

推荐答案

嗯 - 也许不是解决方案。但是有一些想法:我做了一些大数据驱动应用程序的架构。我使用EF作为OR-Mapper。有很多方法可以解决这种克隆问题。但是我不会试图找到一个通用的解决方案(它有点不可能使它完美 - 只需考虑依赖环,所以你最终再次构建EF ......)所以我会寻找一个解决方案来创建特定的新对象对于每种类型,它清楚什么是新的,将引用什么是可选的等等。甚至不要考虑原始图形的真实副本(延迟加载,你可能没有整个图形在内存等等,你可以往返数据库,等等)。

我做了很多没有克隆实体的解决方案,如果你有一个很好的关系模型(也许在数据库端有一些继承)你会得到一些清晰的,键入的新 - 方法(可以使用.NET泛型来实现,以避免重复,类似的结构化对象图)。



所以在你的情况下一个明确的NewOrder方法......需要的参数可以做,它可以是一个UI功能,然后从内存中的现有图形填充参数 - 这样用户将体验此操作作为复制...





只是我的2c ......
Hmm - Maybe not an solution. But a few thoughts: I do the architecture of some big data-driven applications. I use EF as OR-Mapper. There are a lot of ways to solve this "cloning". But I wouldn't try to find a generic solution (it's kind of impossible to make it perfect - just think about dependency-rings, so you end up building EF again…) So I'd go for a solution to create new objects specific for each type where it's clear what will be new, what will be referenced what is optional and so on. Don't even think about real copy of an original graph (what about lazy loading, you may don't have the whole graph in memory etc. you'd do roundtrips to the database and so on).
I did many solutions without cloning of entities, and if you have a good relational model (Maybe with some inheritance on the database-side) you will end up with a few clear, typed "New"-Methods (which coud be implemented using .NET generics to avoid duplication, for similar structured object-graphs).

So in your case a clear "NewOrder" method… with needed parameters would do, it could be an UI-Feature then to fill in the parameters from an existing graph in memory - so that users would experience this operation as "copy"...


Just my 2c...


这篇关于将实体与所有映射实体一起克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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