0

I have a method which writes log information in Db. This method is called in a loop. On the second insert I get the InvalidOperationException:

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.

I checked both SSDL and CSDL: they are Ok. StoreGeneratedPattern is set to Identity. DB is also OK - corresponding id property is identity.

What I found is that after

contex.LogHystory.AddObject(logEntry);
context.SaveChanges();

logEntry.Id still equals 0. So after next call we got two entries with the same Id. Why isn't Identity property updated after SaveChanges ?

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137