实体框架代码优先 DBContext ObjectStateManager 错误

发布于 2024-11-23 19:01:07 字数 1631 浏览 4 评论 0原文

我收到错误“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫烠⑼条掵仅有一顆心 2024-11-30 19:01:07

您可以检查名称是否更改如下

private bool CheckIfNameChanged(TestObject testObject)
{
    return !_unitOfWork.TestObjectRepository
       .Any(x => x.TeamId == testObject.TeamId && x.Name == testObject.Name);
}

You can check if name changed as follows

private bool CheckIfNameChanged(TestObject testObject)
{
    return !_unitOfWork.TestObjectRepository
       .Any(x => x.TeamId == testObject.TeamId && x.Name == testObject.Name);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文