Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Edit: 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.
Updated 5-Nov-18 1:25am
Richard Deeming 6-Jul-17 7:57am    
Cloning the entities won't be enough. You'll also need to reset the keys to let EF / SQL assign new ones.
CodeWraith 6-Jul-17 8:03am    
That is what I feared. I must write specific code that must be adapted when there are changes in the data schema. Forget to do that and we will have a new bug report and an excited customer. Too much of this can make maintaining an application hell.
G3Coder 6-Jul-17 8:06am    

Struggling to follow, but do you need a way to work out what in the DB schema will break your copy - so a unique key for example?

CodeWraith 6-Jul-17 8:19am    
No, we don't. I could easily write code that specifically constructs a clone of the entity and all further entities that are mapped to it. Then, one day someone has to implement a new feature by mapping yet another entity to this schema.

This would be the worse case, because it would probably not break my code as it is, but go on without an error, ignoring the change. That's a bug report waiting to happen.

What I would like is something like an entity.Clone() method, which automatically clones the entity and all entities that are mapped to it and storing them as new rows in the respective tables.
[no name] 4-Nov-18 11:24am    
I wondered, the answers.

1 solution

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...
Share this answer
BillWoodruff 21-Dec-18 7:20am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900