在SaveChanges()之后,不会更新StoreGeneratedPattern设置为Identity的属性 [英] Property with StoreGeneratedPattern set to Identity is not updted after SaveChanges()
问题描述
我有一种在Db中写入日志信息的方法. 在循环中调用此方法.在第二次插入时,我收到InvalidOperationException:
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:
已成功提交对数据库的更改,但是在更新对象上下文时发生了错误. ObjectContext可能处于不一致状态.内部异常消息:AcceptChanges无法继续,因为该对象的键值与ObjectStateManager中的另一个对象发生冲突.在调用AcceptChanges之前,请确保键值是唯一的.
我同时检查了SSDL和CSDL:它们还可以. StoreGeneratedPattern设置为身份". DB也可以-相应的id属性是identity.
I checked both SSDL and CSDL: they are Ok. StoreGeneratedPattern is set to Identity. DB is also OK - corresponding id property is identity.
我发现的是
contex.LogHystory.AddObject(logEntry);
context.SaveChanges();
logEntry.Id仍然等于0.因此,在下一次调用之后,我们获得了两个具有相同ID的条目. 为什么SaveChanges之后没有更新Identity属性?
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 ?
推荐答案
Simple and silly. I forgot about result column bindings =)
这篇关于在SaveChanges()之后,不会更新StoreGeneratedPattern设置为Identity的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!