データベースにいくつかの場所/都市を保存しています。データベースのテーブルに新しい列を1つ追加し、プロジェクトのデータモデルを更新するまで、すべてがうまくいきました。
私の場所/都市 this._entities.SaveChanges();
は、次のような例外をスローするたびに保存されます。
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();
}