1

I’m looking for a solution were I can copy an object with several levels of children, update the primary key (and foreign key of the children) and save it on the DB again. Just so I can have an exact copy of the whole object but with a new key.

I tried using ValueInjuecter and it does create a deep copy but I don’t know how to update the foreign keys of the children to make them point to the newly created object.

I also tried AutoMapper and something similar happens.

Does anyone know how I can get this done?

user441365
  • 3,934
  • 11
  • 43
  • 62
  • if you know how to update the children foreign keys manually, you can edit the DeepCloneInjection and add that functionality – Omu Feb 09 '16 at 21:45

2 Answers2

2

Just write manual code to do the copying. It'll probably take you 30 minutes or so. You can also try using .NET's cloning with a BinaryFormatter, but that won't help with your PK/FK issues.

Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69
0

Probably the best and most performant solution is to create your own library that would use MemberwiseClone recursively for every child.

Nikola.Lukovic
  • 1,256
  • 1
  • 16
  • 33