是否可以在没有 EntityKey 的情况下对从 ObjectContext 检索到的对象进行 MemberwiseClone?我正在使用带有 C# 的 Entity Framework 4.1
如果我尝试更改 Id,则会收到以下异常:
The property 'Id' is part of the object's key information and cannot be modified
如果我尝试将 EntityKey 设置为 null:
The EntityKey property can only be set when the current value of the property is null.
我的代码:
Offer newOffer = offer.ShallowCopy();
// does not work...
newOffer.EntityKey = null;
/ does not work either...
newOffer.Id = Guid.NewGuid()
this._context.Add<Offer>(newOffer);
this._context.SaveChanges();
...
public partial class Offer
{
public Offer ShallowCopy()
{
return (Offer)this.MemberwiseClone();
}
}
有人知道我的问题的简单解决方案吗?