2013-01-12 99 views
0

我在我的數據庫中保存了一些位置/城市。事情是一切工作正常,直到我添加一個新的列到數據庫中的表,並更新我的項目中的數據模型。實體SaveChanges拋出異常

我的位置/城市被打救g雖然我this._entities.SaveChanges();拋出去的東西像一個execption:

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message:AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

我真的不知道什麼,因爲它的問題之前,工作,這種變化應該不會影響它真的...

這裏有一些代碼:
首先我把所有的對象都放到currentLocation中。 我通過foreach將它們循環添加到方法Add()中。 最後我呼籲_entites.SaveChanges()。一切都被保存,但我得到一個錯誤......

  var webService = new WeatherWebService(); 
      currentLocation = webService.FindLocation(city); 

      // ...save the user in the database. 
      foreach (var item in currentLocation) 
      { 
       this._repository.Add(item); 
      } 

      this._repository.Save(); 

    public override void Add(Location location) 
    { 
     this._entities.Locations.AddObject(location); 
    } 

    public override void Save() 
    { 
     this._entities.SaveChanges(); 
    } 
+2

您必須發佈(摘要)類和可能的代碼。 –

回答

0

我懷疑可能發生的異常是由於兩個不同的問題:

1-在以前的調用,你已經檢索到的位置;並且您正嘗試插入相同的值。 2-從Web服務檢索到的位置實體的主鍵不是唯一的。

爲了確定確切的問題,請檢查顯示的異常的內部異常。

+0

Im在數據庫中使用自動增量。它被插入但仍然出現錯誤...內部異常爲空 – Rovdjuret