我正在使用实体框架来填充网格控件。有时当我进行更新时,我得到以下错误:
存储更新、插入或删除语句影响了意外的行数(0)。实体可能在加载实体后已被修改或删除。刷新ObjectStateManager条目。
我不知道怎么复制这个。但这可能和我更新的时间间隔有关系。有人见过这个吗,或者有人知道错误消息指的是什么吗?
编辑:不幸的是,我不再有自由重现我在这里遇到的问题,因为我离开了这个项目,不记得我最终是否找到了解决方案,是否有其他开发人员修复了它,或者是否我绕过了它。因此我不能接受任何回答。
我正在使用实体框架来填充网格控件。有时当我进行更新时,我得到以下错误:
存储更新、插入或删除语句影响了意外的行数(0)。实体可能在加载实体后已被修改或删除。刷新ObjectStateManager条目。
我不知道怎么复制这个。但这可能和我更新的时间间隔有关系。有人见过这个吗,或者有人知道错误消息指的是什么吗?
编辑:不幸的是,我不再有自由重现我在这里遇到的问题,因为我离开了这个项目,不记得我最终是否找到了解决方案,是否有其他开发人员修复了它,或者是否我绕过了它。因此我不能接受任何回答。
当前回答
Got the same problem when removing item from table(ParentTable) that was referenced by another 2 tables foreign keys with ON DELETE CASCADE rule(RefTable1, RefTable2). The problem appears because of "AFTER DELETE" trigger at one of referencing tables(RefTable1). This trigger was removing related record from ParentTable as result RefTable2 record was removed too. It appears that Entity Framework, while in-code was explicitly set to remove ParentTable record, was removing related record from RefTable1 and then record from RefTable2 after latter operation this exception was thrown because trigger already removed record from ParentTable which as results removed RefTable2 record.
其他回答
Got the same problem when removing item from table(ParentTable) that was referenced by another 2 tables foreign keys with ON DELETE CASCADE rule(RefTable1, RefTable2). The problem appears because of "AFTER DELETE" trigger at one of referencing tables(RefTable1). This trigger was removing related record from ParentTable as result RefTable2 record was removed too. It appears that Entity Framework, while in-code was explicitly set to remove ParentTable record, was removing related record from RefTable1 and then record from RefTable2 after latter operation this exception was thrown because trigger already removed record from ParentTable which as results removed RefTable2 record.
我得到了同样的错误,因为PK的一部分是datetime列,而插入的记录使用了datetime。现在作为这一列的值。实体框架将以毫秒级精度插入值,然后以毫秒级精度查找刚刚插入的值。然而,SqlServer将该值舍入到秒精度,因此实体框架无法找到毫秒精度值。
解决方案是截断DateTime中的毫秒。在插入之前。
当公认的答案说“它不会最终覆盖应用程序不知道已经发生的更改”时,我很怀疑,因为我的对象是新创建的。但后来发现,有一个INSTEAD OF UPDATE, INSERT- TRIGGER附加到表中,它正在更新同一表的计算列。
一旦我将其更改为AFTER INSERT, UPDATE,它就可以正常工作了。
检查你是否忘记了GridView中的“DataKeyNames”属性。 当在GridView中修改数据时,它是必须的
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
我也面临着同样可怕的错误……:)然后我意识到我忘记设置一个
@Html。HiddenFor(model => model. userprofile . userid)
用于更新对象的主键!我容易忘记这个简单但非常重要的事情!
顺便说一下:HiddenFor是ASP的。净MVC。