实体框架:“存储更新、插入或删除语句影响了意外的行数 (0)." [英] Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)."

查看:23
本文介绍了实体框架:“存储更新、插入或删除语句影响了意外的行数 (0)."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架来填充网格控件.有时,当我进行更新时,我会收到以下错误:

I am using Entity Framework to populate a grid control. Sometimes when I make updates I get the following error:

存储更新、插入或删除语句影响了意外的行数 (0).自加载实体以来,实体可能已被修改或删除.刷新 ObjectStateManager 条目.

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

我不知道如何重现这个.但这可能与我进行更新的紧密程度有关.有没有人看到这个或有谁知道错误消息指的是什么?

I can't figure out how to reproduce this. But it might have something to do with how close together I make the updates. Has anyone seen this or does anyone know what the error message refers to?

不幸的是,我不再能够自由地重现我在这里遇到的问题,因为我离开了这个项目并且不记得我是否最终找到了解决方案,是否有其他开发人员修复了它,或者我是否工作过周围.因此我不能接受任何答案.

Unfortunately I am no longer at liberty to reproduce the problem I was having here, because I stepped away from this project and don't remember if I eventually found a solution, if another developer fixed it, or if I worked around it. Therefore I cannot accept any answers.

推荐答案

这是一种称为乐观并发的功能的副作用.

That's a side-effect of a feature called optimistic concurrency.

不是 100% 确定如何在 Entity Framework 中打开/关闭它,但基本上它告诉您的是,在您从数据库中获取数据和保存更改之间,其他人已经更改了数据(这意味着当你去保存它时,实际上更新了 0 行).在 SQL 术语中,他们的 update 查询的 where 子句包含行中每个字段的原始值,如果有 0 行受到影响,它就知道出了问题.

Not 100% sure how to turn it on/off in Entity Framework but basically what it's telling you is that between when you grabbed the data out of the database and when you saved your changes someone else has changed the data (Which meant when you went to save it 0 rows actually got updated). In SQL terms, their update query's where clause contains the original value of every field in the row, and if 0 rows are affected it knows something's gone wrong.

其背后的想法是,您最终不会覆盖您的应用程序不知道发生的更改 - 这基本上是 .NET 在您的所有更新中抛出的一个小安全措施.

The idea behind it is that you won't end up overwriting a change that your application didn't know has happened - it's basically a little safety measure thrown in by .NET on all your updates.

如果它是一致的,那么它很可能发生在您自己的逻辑中(例如:您实际上是在选择和更新之间以另一种方法自己更新数据),但这可能只是两个应用程序之间的竞争条件.

If it's consistent, odds are it's happening within your own logic (EG: You're actually updating the data yourself in another method in-between the select and the update), but it could be simply a race condition between two applications.

这篇关于实体框架:“存储更新、插入或删除语句影响了意外的行数 (0)."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆