1
當我用這個方法更新的元素,我得到異常:例外設置方法
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
這是方法:
public void Set(TaskPrice entity)
{
bool isExists = GetQuery().Any(x => x.TaskId == entity.TaskId);
if (isExists)
{
ObjectStateEntry entry=null;
if (this.Context.ObjectStateManager.TryGetObjectStateEntry(entity, out entry) == false)
{
this.ObjectSet.Attach(entity);
}
this.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}
else
{
this.ObjectSet.AddObject(entity);
}
}
我undertand這個例外accurs因爲GetQuery().Any(x => x.TaskId == entity.TaskId);
attachs的元素從數據庫,當我附加更新的實體時,它是有附加元素具有相同的ID。
我該如何解決這個問題,使方法更新?
哪條線拋出該異常? – 2011-05-05 12:58:31