我有这个扩展方法来克隆我的 LINQ To SQL 对象:
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);
}
}
但是,当我携带未加载所有引用的对象时,在使用 DataLoadOptions 查询时,有时它会抛出对象已处理的异常,但问题是我不要求未加载的引用(null)。
例如,我有很多引用的客户,我只需要保存地址引用 EntityRef<> 并且我不加载任何其他内容。但是,当我克隆对象时,此异常迫使我使用 Customer 对象加载所有 EntitySet<> 引用,这可能太多并减慢应用程序速度。
有什么建议么?