【发布时间】:2012-10-17 00:34:11
【问题描述】:
相关:Set Properties that not null using linq and reflection
各位专家
我更改了上面链接中的代码:
public static void MyCopy<T>(this T src, T dest)
{
var notNullProps = typeof(T).GetProperties()
.Where(x => x.GetValue(src, null) != null);
foreach (var p in notNullProps)
{
p.SetValue(dest, p.GetValue(src, null),null);
}
}
我写了这段代码来复制属性:
NorthwindModel1.Order ord1 = new NorthwindModel1.Order() {CustomerID="Nima",Freight=1.33m,ShipCity="Agha" };
NorthwindModel1.Order ord2 = new NorthwindModel1.Order() ;
ord1.MyCopy(ord2);
但我收到了这个错误:
EntityReference 已经被初始化。 InitializeRelatedReference 只能用于在实体对象的反序列化期间初始化新的 EntityReference。
请帮我解决这个问题
【问题讨论】:
-
查看此页面上的内容/cmets:urmanet.ch/?p=11 此错误似乎与外键引用有关。在评论中是一个可能的解决方案。该页面还描述了实体对象的克隆/复制,因此无论如何这对您来说可能是值得一读的。
-
您是否尝试过使用 BinaryFormatter 进行克隆?
标签: c# linq reflection entity-framework-4