"The store update, insert, or delete statement impacted an unexpected amount of rows (0)," says Entity Framework.

entity-framework

Question

I'm populating a grid control with data using Entity Framework. I sometimes have the following problem when I make updates:

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.

It's impossible for me to duplicate this. However, it can be related to how often I provide updates. Does anybody have any knowledge about this or what the error message relates to?

Because I left this project, I can no longer duplicate the issue I was experiencing because I can't recall if I finally discovered a solution, whether another developer repaired it, or whether I found a workaround. I will thus not accept any responses.

1
321
8/6/2018 3:38:31 AM

Accepted Answer

That is a consequence of an attribute known as optimistic concurrency.

Not sure how to turn it on or off in Entity Framework, but it simply informs you that someone else modified the data between the time you took it out of the database and the time you saved your modifications (Which meant when you went to save it 0 rows actually got updated). SQL-speak, theyupdate query's where clause includes each field's original value, and if no rows are impacted, it knows something is wrong.

It's just a little safety precaution put in by.NET on all your updates with the hope that you won't wind up overwriting a modification that your application wasn't aware had occurred.

It may just be a race situation between two programs, but if it's consistent, chances are it's occurring inside your own logic (for example, you're really altering the data yourself in another function between the select and the update).

197
10/31/2014 6:51:39 PM

Popular Answer

This problem I encountered was brought on by the entity's ID (key) field not being set. As a result, when the context tried to store the data, it was unable to locate an ID of 0. Place a breakpoint in your update statement and make sure the entity's ID has been set before continuing.

From the remark of Paul Bellora

I had this exact issue, caused by forgetting to include the hidden ID input in the .cshtml edit page



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow