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

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

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

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


当前回答

我将抛出这个,以防有人在并行循环中工作时遇到这个问题:

Parallel.ForEach(query, deet =>
{
    MyContext ctx = new MyContext();
    //do some stuff with this to identify something
    if(something)
    {
         //Do stuff
         ctx.MyObjects.Add(myObject);
         ctx.SaveChanges() //this is where my error was being thrown
    }
    else
    {
        //same stuff, just an update rather than add
    }
}

我把它改成如下:

Parallel.ForEach(query, deet =>
{
    MyContext ctxCheck = new MyContext();
    //do some stuff with this to identify something
    if(something)
    {
         MyContext ctxAdd = new MyContext();
         //Do stuff
         ctxAdd .MyObjects.Add(myObject);
         ctxAdd .SaveChanges() //this is where my error was being thrown
    }
    else
    {
        MyContext ctxUpdate = new MyContext();
        //same stuff, just an update rather than add
        ctxUpdate.SaveChanges();
    }
}

不确定这是否是“最佳实践”,但它通过让每个并行操作使用自己的上下文来解决我的问题。

其他回答

如果您试图在edmx文件中创建到“function Imports”的映射,则可能会导致此错误。只要清除位于edmx中给定实体的映射细节中的插入、更新和删除字段,它就应该工作了。 我希望我讲清楚了。

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

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.

这件事刚刚发生在我身上。我正在运行Aurora (AWS MySQL),并试图将记录添加到表中。在模型中标记为[Key]的字段被映射到表中的一个自动递增字段…至少我是这么想的。它被设置为主键,但没有设置为自动递增。所以将它设置为自动递增解决了我的问题。

我得到了同样的错误,因为PK的一部分是datetime列,而插入的记录使用了datetime。现在作为这一列的值。实体框架将以毫秒级精度插入值,然后以毫秒级精度查找刚刚插入的值。然而,SqlServer将该值舍入到秒精度,因此实体框架无法找到毫秒精度值。

解决方案是截断DateTime中的毫秒。在插入之前。

我将抛出这个,以防有人在并行循环中工作时遇到这个问题:

Parallel.ForEach(query, deet =>
{
    MyContext ctx = new MyContext();
    //do some stuff with this to identify something
    if(something)
    {
         //Do stuff
         ctx.MyObjects.Add(myObject);
         ctx.SaveChanges() //this is where my error was being thrown
    }
    else
    {
        //same stuff, just an update rather than add
    }
}

我把它改成如下:

Parallel.ForEach(query, deet =>
{
    MyContext ctxCheck = new MyContext();
    //do some stuff with this to identify something
    if(something)
    {
         MyContext ctxAdd = new MyContext();
         //Do stuff
         ctxAdd .MyObjects.Add(myObject);
         ctxAdd .SaveChanges() //this is where my error was being thrown
    }
    else
    {
        MyContext ctxUpdate = new MyContext();
        //same stuff, just an update rather than add
        ctxUpdate.SaveChanges();
    }
}

不确定这是否是“最佳实践”,但它通过让每个并行操作使用自己的上下文来解决我的问题。