Entity Framework: “Store update, insert, or delete statement affected an unexpected number of rows (0).” [closed]

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

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?

Edit: 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.

45 Answers
45

I ran into this and it was caused by the entity’s ID (key) field not being set. Thus when the context went to save the data, it could not find an ID = 0. Be sure to place a break point in your update statement and verify that the entity’s ID has been set.

From Paul Bellora’s comment

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

Leave a Comment