我在我的數據庫中保存了一些位置/城市。事情是一切工作正常,直到我添加一個新的列到數據庫中的表,並更新我的項目中的數據模型。實體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();
}
您必須發佈(摘要)類和可能的代碼。 –