Clone LINQ To SQL object Extension Method throws object dispose exception....
Posted
by gtas
on Stack Overflow
See other posts from Stack Overflow
or by gtas
Published on 2010-06-15T21:22:39Z
Indexed on
2010/06/16
11:42 UTC
Read the original article
Hit count: 274
Hello all,
I have this extension method for cloning my LINQ To SQL objects:
public static T CloneObjectGraph<T>(this T obj) where T : class
{
var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);
using (var ms = new System.IO.MemoryStream())
{
serializer.WriteObject(ms, obj);
ms.Position = 0;
return (T)serializer.ReadObject(ms);
}
}
But while i carry objects with not all references loaded, while qyuerying with DataLoadOptions, sometimes it throws the object disposed exception, but thing is I don't ask for references that is not loaded (null).
e.g. I have Customer with many references and i just need to carry on memory the Address reference EntityRef<> and i don't Load anything else. But while i clone the object this exception forces me to load all the EntitySet<> references with the Customer object, which might be too much and slow down the application speed.
Any suggestions?
© Stack Overflow or respective owner