Property with StoreGeneratedPattern set to Identity is not updted after SaveChanges()

╄→尐↘猪︶ㄣ 提交于 2020-01-17 07:30:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!