我在使用幫助器方法對一組模型對象執行更新時遇到了一些麻煩。該表使用查找表爲每個代理/用戶保存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.
這行代碼'db.RelationshipCodeLookup.Attach(relCode);'給了我同樣的「使用相同的密鑰對象已存在」,我是越來越錯誤。 – NealR