0

データベースにいくつかの場所/都市を保存しています。データベースのテーブルに新しい列を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();
    }
4

1 に答える 1

0

2 つの異なる問題が原因で例外が発生する可能性があると思われます。

1- 以前の通話で、すでに場所を取得しています。同じ値を挿入しようとしています。2- Web サービスから取得したロケーション エンティティの主キーが一意ではありません。

正確な問題を特定するには、表示された例外の内部例外を確認してください。

于 2013-01-12T11:09:59.607 に答える