我有如下一段代码
public void UpdateRC_User(RC_User currentRC_User)
{
this.ObjectContext.RC_User.AttachAsModified(currentRC_User, this.ChangeSet.GetOriginal(currentRC_User));
}
RC_User 对象包涵一个子Entity集合,当修改RC_User时(不包括对子Entity集合的修改),以上代码能正常运行,如果对子Entity做了新增或删除元素时,执行会报origin为null的异常,跟踪发现是this.ChangeSet.GetOriginal()这个方法返回值为空。
然后修改代码如下,直接到数据库中取origin
RC_User originUser = ObjectContext.RC_User.Where(u => u.iUserID == currentRC_User.iUserID).FirstOrDefault();
this.ObjectContext.RC_User.AttachAsModified(currentRC_User, originUser );
又报这个错
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
请在在一对多的情况下,我要如何才能保存成功呢?