我们在项目中使用EntityFramework。我有以下代码:
var realType = ObjectContext.GetObjectType(entity.GetType());
if (this.Entry(entity).State == EntityState.Detached)
{
this.Set(realType).Attach(entity);
}
在我的数据库上下文类中抛出异常:
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
为什么在我检查实体分离后会发生这种情况?我该如何解决这个问题?