0

我在我的数据库中保存了一些位置/城市。一切正常,直到我在数据库中的表中添加了一个新列并更新了项目中的数据模型。

我的位置/城市确实会通过我 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

我怀疑由于两个不同的问题可能会发生异常:

1- 在之前的通话中,您已经检索到位置;并且您正在尝试插入相同的值。2- 从 Web 服务检索的位置实体的主键不是唯一的。

为了确定确切的问题,请检查显示异常的内部异常。

于 2013-01-12T11:09:59.607 回答