问题
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 ?
回答1:
http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/12/17/ado-net-entity-framework-tools-stored-procedures.aspx
Simple and silly. I forgot about result column bindings =)
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/21fdd60d-4f2a-4f2f-9e35-588a97b2e411/
来源:https://stackoverflow.com/questions/8152736/property-with-storegeneratedpattern-set-to-identity-is-not-updted-after-savechan