How can I make a deep copy of an object graph that has been loaded by Entity Framework Code First with proxies enabled?
I'm using code like this:
static public T DeepCopy<T>(T obj)
{
BinaryFormatter s = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
s.Serialize(ms, obj);
ms.Position = 0;
T t = (T)s.Deserialize(ms);
return t;
}
}
However, the serializer complains correctly that the proxy types are not known types.
UPDATE
Using DataContractSerializer with ProxyContractResolver does not work because my class is decorated with DataContract(IsReference = true)
.
See
Serialize EF Proxy when POCO has IsReference = true Attribute