1

我在使用辅助方法对一组模型对象执行更新时遇到了一些麻烦。该表使用查找表来保存每个代理/用户 5 条记录。如果我想为代理保存记录,我需要将该记录保存到AgentTransmission表中,并在表中保存最多 5 条其他记录RelationshipCodeLookup

由于我必须为每个代理执行五次,并且我们必须在 Create 和 Edit 方法中执行此过程,因此我创建了一个帮助方法来保存记录。这在创建过程中工作正常,因为我们只是在做一个DbContext.Add(). 但是,当我需要执行更新时,我会收到错误消息

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

我认为这与我将模型对象传递给我的辅助方法这一事实有关,因此DbContext认为它有两个单独的对象要跟踪。我这样说是因为被注释掉的代码行工作得很好,并允许我保存对象。但是,将对象传递给辅助方法会得到上述错误。

有谁知道解决这个问题的方法(使用辅助方法执行更新)?

控制器动作

//Save relationship codes in lookup table
if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode1))
{
    //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
    //db.SaveChanges();
    SaveRelationshipCodes(agenttransmission.RelationshipCode1, agenttransmission.ID);
}

if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode2))
{
    //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
    //db.SaveChanges();
    SaveRelationshipCodes(agenttransmission.RelationshipCode2, agenttransmission.ID);
}

if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode3))
{
    //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
    //db.SaveChanges();
    SaveRelationshipCodes(agenttransmission.RelationshipCode3, agenttransmission.ID);
}

if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode4))
{
    //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
    //db.SaveChanges();
    SaveRelationshipCodes(agenttransmission.RelationshipCode4, agenttransmission.ID);
}

if (AgentTransmissionValidator.ValidateRelationshipCode(agenttransmission.RelationshipCode5))
{
    //db.Entry(agenttransmission.RelationshipCode1).State = EntityState.Modified;
    //db.SaveChanges();
    SaveRelationshipCodes(agenttransmission.RelationshipCode5, agenttransmission.ID);
}

辅助方法

    public void SaveRelationshipCodes(RelationshipCodeLookup relCode, int id)
    {
        if (relCode.AgentId == 0) relCode.AgentId = id;

        relCode.LastChangeDate = DateTime.Now;
        relCode.LastChangeId = Security.GetUserName(User);

        //Check to see if record exists and if not add it
        if (db.RelationshipCodeLookup.Find(id, relCode.RelCodeOrdinal) != null)
        {
            db.Entry(relCode).State = EntityState.Detached;
        }
        else
        {
            if(relCode.RelCodeOrdinal == 0) relCode.RelCodeOrdinal = FindOrdinal(relCode);
            db.RelationshipCodeLookup.Add(relCode);
        }

        db.SaveChanges();
    }

编辑

在浏览网页后,我尝试通过这种方法保存

        //Check to see if record exists and if not add it
        if (db.RelationshipCodeLookup.Find(id, relCode.RelCodeOrdinal) != null)
        {
            db.Entry(relCode).CurrentValues.SetValues(relCode);
        }
        else
        {

Member 'CurrentValues' cannot be called for the entity of type 'RelationshipCodeLookup because the entity does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet<RelationshipCodeLookup>

但是....这样做只会让我回到开头并出现以下错误db.RelationshipCodeLookup.Attach(relCode);

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
4

2 回答 2

0

试试这个:

db.RelationshipCodeLookup.Attach(relCode);
db.Entry(relCode).State = EntityState.Modified;

对于要附加分离对象的更新,然后将其状态设置为已修改。

于 2013-11-14T22:56:04.203 回答
0

这里的问题似乎是实体框架无法同时跟踪两个相同类型的对象。因此,我发现这个问题的解决方案有点奇怪。通过调用.Find()DbContext实例化模型对象的第二个副本,我终于能够保存。似乎打破了 EF 在错误消息中为我制定的所有规则,但是嘿它有效。

    public void SaveRelationshipCodes(int id, RelationshipCodeLookup relCode)
    {
        if (relCode.AgentId == 0) relCode.AgentId = id;

        relCode.LastChangeDate = DateTime.Now;
        relCode.LastChangeId = Security.GetUserName(User);

        //Check to see if record exists and if not add it
        if (db.RelationshipCodeLookup.Find(id, relCode.RelCodeOrdinal) != null)
        {
            //Need to call .Find to get .CurrentValues method call to work
            RelationshipCodeLookup dbRelCode = db.RelationshipCodeLookup.Find(id, relCode.RelCodeOrdinal); 
            db.Entry(dbRelCode).CurrentValues.SetValues(relCode);
        }
        else
        {
            if(relCode.RelCodeOrdinal == 0) relCode.RelCodeOrdinal = FindOrdinal(relCode);
            db.RelationshipCodeLookup.Add(relCode);
        }

        db.SaveChanges();
    }
于 2013-11-15T16:41:45.423 回答