New in MVC / EF ... My MVC web application was created using code first from a database model, since I already had a database (SQLExpress) ...
I get the following error when trying to update a record in the database:
Save expression about updating, insertion or deletion caused by an unexpected number of rows (0). Objects may be modified or deleted as objects are loaded
I got this error when using standard code generated using code first from the Entity Framework database. Strange, because I'm working on an autonomous machine that no one has access to.
Here is the code that didn't work:
if (ModelState.IsValid)
{
db.Entry(tblPropGroup).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
, , :
if (ModelState.IsValid)
{
db.Entry(_Prop).State = EntityState.Modified;
bool saveFailed;
do
{
saveFailed = false;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
var entry = ex.Entries.Single();
entry.OriginalValues.SetValues(entry.GetDatabaseValues());
}
} while (saveFailed);
- , db.SaveChanges DbUpdateConcurrencyException ? , - . 22 , , .