我在我的数据库中保存了一些位置/城市。
我的位置/城市确实会通过我 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();
}