New to MVC/EF... My MVC web app was created using the code first from database model since I already had the database (SQLExpress)...
I'm getting the following error when I try an update a record to the database:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded
I got this error when using the standard code generated using the code first from database Entity Framework model. Strange because I'm working on a standalone machine which no one else has accessing to.
Here is the code that was failing:
if (ModelState.IsValid)
{
db.Entry(tblPropGroup).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
So I wrote the following work around, which worked flawlessly for a while:
if (ModelState.IsValid)
{
db.Entry(_Prop).State = EntityState.Modified;
bool saveFailed;
do
{
saveFailed = false;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
// Update original values from the database
var entry = ex.Entries.Single();
entry.OriginalValues.SetValues(entry.GetDatabaseValues());
}
} while (saveFailed);
Can anyone explain why db.SaveChanges
results in a DbUpdateConcurrencyException
on a DB with no other users? It makes no difference if I make any changes to the data or not. I have 22 different tables I'm access in this program, about half won't work without this workaround.