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.
Check whether you forgot the "DataKeyNames" attribute in the GridView. it's a must when modifying data within the GridView
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
i had the same problem, i figure out that was caused by the RowVersion which was null. Check that your Id and your RowVersion are not null.
for more information refer to this tutorial
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application
I was facing this same scaring error... :) Then I realized that I was forgetting to set a
@Html.HiddenFor(model => model.UserProfile.UserId)
for the primary key of the object being updated! I tend to forget this simple, but very important thingy!
By the way:
HiddenFor
is for ASP.NET MVC.I got that error when I was deleting some rows in the DB (in the loop), and the adding the new ones in the same table.
The solutions for me was, to dynamicaly create a new context in each loop iteration
While editing include the id or primary key of the entity as a hidden field in the view
ie
that solves the problem.
Also if your model includes non-used item include that too and post that to the controller
I got this error sporadically when using an
async
method. Has not happened since I switched to a synchronous method.Errors sporadically:
Works all the time: