2013-11-14 26 views
1

我在使用幫助器方法對一組模型對象執行更新時遇到了一些麻煩。該表使用查找表爲每個代理/用戶保存5條記錄。如果我想保存該代理的記錄,則需要將該記錄保存到AgentTransmission表中,並在RelationshipCodeLookup表中最多存儲5條其他記錄。將實體框架對象傳遞給幫助器方法以進行更新

由於我必須爲每個代理執行五次這樣的操作,並且我們必須在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); 
} 

helper方法

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. 

回答

0

這裏的問題似乎是Entity Framework不能同時跟蹤兩個同類的對象。正因爲如此,我覺得這個問題的解決方法不止有點奇怪。通過在DbContext上調用.Find()並實例化模型對象的第二個副本,我終於能夠保存。似乎打破了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(); 
    } 
0

試試這個:

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

對於更新要附加的分離對象,然後將其設置爲修改狀態。

+0

這行代碼'db.RelationshipCodeLookup.Attach(relCode);'給了我同樣的「使用相同的密鑰對象已存在」,我是越來越錯誤。 – NealR