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.