实体框架代码优先 DBContext ObjectStateManager 错误
我收到错误“ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。
”
public class TestObject
{
public string Name {get;set;}
}
public EditTeamResponse Edit(TestObject testObject)
{
if (!ValidateTestObject(testObject))
{
return testObject;
}
try
{
_unitOfWork.TestObjectRepository.Update(testObject);
_unitOfWork.Commit();
}
catch (Exception)
{
//Error is thrown here
_validationDictionary.AddError("Unknown", "Unknown Error!");
return testObject;
}
// Other Extra Code
return editTeamResponse;
}
protected bool ValidateTestObject(TestObject testObject)
{
if (CheckIfNameChanged(teamToValidate))
{
if (_unitOfWork.TestObjectRepository.Any(x => x.Name == testObject.Name))
_validationDictionary.AddError("Name", "Name already exist.");
}
return _validationDictionary.IsValid;
}
private bool CheckIfNameChanged(TestObject testObject)
{
return _unitOfWork.TestObjectRepository.FindBy(testObject.TeamId).Name != testObject.Name;
}
我知道当我调用 CheckIfNameChanged(TestObject testObject) 方法时,我向 ObjectContext 添加了一个实体键,当我调用 _unitOfWork.TestObjectRepository.Update(testObject) 时附加或编辑代码时:
public void Update{
_context.Entry(entity).State = EntityState.Modified;
}
这就是冲突发生的地方,我在 ObjectStateManager 中得到了两个相同的实体键。有没有办法解决这个问题,而无需我去上下文分离实体,或者还有其他方法吗?将实体与上下文分离的最佳方法是什么?
I am getting an Error "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
"
public class TestObject
{
public string Name {get;set;}
}
public EditTeamResponse Edit(TestObject testObject)
{
if (!ValidateTestObject(testObject))
{
return testObject;
}
try
{
_unitOfWork.TestObjectRepository.Update(testObject);
_unitOfWork.Commit();
}
catch (Exception)
{
//Error is thrown here
_validationDictionary.AddError("Unknown", "Unknown Error!");
return testObject;
}
// Other Extra Code
return editTeamResponse;
}
protected bool ValidateTestObject(TestObject testObject)
{
if (CheckIfNameChanged(teamToValidate))
{
if (_unitOfWork.TestObjectRepository.Any(x => x.Name == testObject.Name))
_validationDictionary.AddError("Name", "Name already exist.");
}
return _validationDictionary.IsValid;
}
private bool CheckIfNameChanged(TestObject testObject)
{
return _unitOfWork.TestObjectRepository.FindBy(testObject.TeamId).Name != testObject.Name;
}
I know that when I call CheckIfNameChanged(TestObject testObject) method i added an Entity Key to the ObjectContext and when i attach or edit the code when i call the _unitOfWork.TestObjectRepository.Update(testObject):
public void Update{
_context.Entry(entity).State = EntityState.Modified;
}
This is where the conflict happen and i got two same entity key in the ObjectStateManager. Is there a way to solve this problem without me going to the Context to detach the entity or is there some other way? And what is the best way to detach an entity from the context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查名称是否更改如下
You can check if name changed as follows