【发布时间】:2009-07-15 14:34:04
【问题描述】:
我正在尝试将实体附加到 ObjectContext。 当我这样做时,会抛出以下 InvalidOperationException:
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same key.
我在对象状态管理器中检查,该项目不存在:
//Data context is actually the object context.
ObjectStateEntry contact;
while ( //Should only work once since it should be true if the item was attached
!DataContext.ObjectStateManager.
TryGetObjectStateEntry(Contact, out contact)
)
DataContext.Attach(Contact); //Here is the exception thrown.
或者看看这个抽象的例子,告诉我它是否有意义:
EntityState state = Contact.EntityState; //Detached
DataContext.Attach(Contact); //Throws the exception.
DataContext.AttachTo("Entities.Contacts", Contact); //Throws the Exception
var detached = DataContext.ObjectStateManager.
GetObjectStateEntries(EntityState.Detached);
//InvalidArgumentException - detached entities cannot be in the obj state mgr
也欢迎用VB回答。
【问题讨论】:
标签: entity-framework concurrency objectstatemanager