[Solved] Clone Enity framework POCO


I found a nice turn around .

If you try MemberwiseClone() to get a copy of the object and then add the object to the context and SaveChanges() you will get an error because the
MemberwiseClone() does clone the Dynamic Proxy too .

So my turn around is

Create a new Context just for the cloning purposes .
then do

ctx.Configuration.ProxyCreationEnabled = false;

Customer copy = ctxClone.Customers.Find( 2).Copy()

Copy here is the method you defined that just calls MemberwiseClone underneath .

then do

ctx.Customers.Add(copy);
ctx.SaveChanges() ;

This is the closest route i found to handle this problem .

If you prefer the reflection solution then go for it .

Fyi if you ProxyCreationEnabled = false in your other code you will loose change tracking and lazy loading , so be careful about it .

solved Clone Enity framework POCO