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

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

我不知道怎么复制这个。但这可能和我更新的时间间隔有关系。有人见过这个吗,或者有人知道错误消息指的是什么吗?

编辑:不幸的是,我不再有自由重现我在这里遇到的问题,因为我离开了这个项目,不记得我最终是否找到了解决方案,是否有其他开发人员修复了它,或者是否我绕过了它。因此我不能接受任何回答。


当前回答

我在一个缺少主键并且有一个DATETIME(2,3)列的表上遇到了这个问题(因此实体的“主键”是所有列的组合)…执行插入时,时间戳有一个更精确的时间(2018-03-20 08:29:51.8319154),被截断为(2018-03-20 08:29:51.832),因此对关键字段的查找失败。

其他回答

我有这个问题与Mvc身份注册新用户,而不是:

var result = await UserManager.CreateAsync(user);

我正在做:

var result = await UserManager.UpdateAsync(user);

在SqlServer环境中调试这个问题的一种方法是使用SqlServer副本中包含的Sql Profiler,或者如果使用Express版本,可以通过下面的链接从CodePlex免费获得Express Profiler的副本:

表达分析器

通过使用Sql Profiler,您可以访问EF发送到DB的任何内容。就我而言,这相当于:

exec sp_executesql N'UPDATE [dbo].[Category]
SET [ParentID] = @0, [1048] = NULL, [1033] = @1, [MemberID] = @2, [AddedOn] = @3
WHERE ([CategoryID] = @4)
',N'@0 uniqueidentifier,@1 nvarchar(50),@2 uniqueidentifier,@3 datetime2(7),@4 uniqueidentifier',
@0='E060F2CA-433A-46A7-86BD-80CD165F5023',@1=N'I-Like-Noodles-Do-You',@2='EEDF2C83-2123-4B1C-BF8D-BE2D2FA26D09',
@3='2014-01-29 15:30:27.0435565',@4='3410FD1E-1C76-4D71-B08E-73849838F778'
go

我复制粘贴到Sql Server的查询窗口并执行它。可以肯定的是,尽管它运行了,但是0条记录受到了这个查询的影响,因此EF返回了错误。

在我的例子中,问题是由CategoryID引起的。

没有发送到数据库的ID EF标识的CategoryID,因此0条记录受到影响。

这并不是EF的错,而是在视图控制器中错误的空合并“??”语句,将无意义的内容发送到数据层。

我也面临着同样可怕的错误……:)然后我意识到我忘记设置一个

@Html。HiddenFor(model => model. userprofile . userid)

用于更新对象的主键!我容易忘记这个简单但非常重要的事情!

顺便说一下:HiddenFor是ASP的。净MVC。

我也有同样的问题,我发现这是由RowVersion为null引起的。 检查Id和RowVersion是否为空。

有关更多信息,请参阅本教程

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application

该问题是由以下两种原因之一引起的:-

You tried to update a row with one or more properties are Concurrency Mode: Fixed .. and the Optimistic Concurrency prevented the data from being saved. Ie. some changed the row data between the time you received the server data and when you saved your server data. You tried to update or delete a row but the row doesn't exist. Another example of someone changing the data (in this case, removing) in between a retrieve then save OR you're flat our trying to update a field which is not an Identity (ie. StoreGeneratedPattern = Computed) and that row doesn't exist.