Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to edit db entries using EFCore, EntityState.Modified: "Database operation expected to affect 1 row(s) but actually affected 0 row(s)."

Tags:

I'm using Identity Core 1.0 with ASP.NET MVC Core 1.0 and Entity Framework Core 1.0 to create a simple user registration system with this article as a starting point, and I am trying to add user roles. I can add user roles, but I'm unable to edit them. Here is the Edit action in the RolesController:

    [HttpPost]     [ValidateAntiForgeryToken]     public IActionResult Edit(IdentityRole role)     {         try         {             _db.Roles.Attach(role);             _db.Entry(role).State = Microsoft.EntityFrameworkCore.EntityState.Modified;             _db.SaveChanges();             return RedirectToAction("Index");         }         catch (Exception ex)         {             Console.WriteLine(ex);             return View();         }     } 

Here is the form in the corresponding view:

@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole @{     ViewBag.Title = "Edit"; }  <h2>Edit Role</h2> <hr /> @using (Html.BeginForm()) {     @Html.AntiForgeryToken()     @Html.ValidationSummary(true)     @Html.HiddenFor(model => model.Id)     <div>Role name</div>     <p>@Html.TextBoxFor(model => model.Name)</p>     <input type="submit" value="Save" /> } 

The new role name does not save to the database, and I get the following exception: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded.

I was able to use this exact code (with the Microsoft.AspNet.Identity.EntityFramework dependency instead of EntityFrameworkCore) to edit database entries using EF 7, Identity 3, etc.

Any thoughts on why this code will not allow database entries to be modified?

like image 944
jmk22 Avatar asked Sep 13 '16 00:09

jmk22


People also ask

What does EntityState Modified do?

State = EntityState. Modified; , you are not only attaching the entity to the DbContext , you are also marking the whole entity as dirty. This means that when you do context. SaveChanges() , EF will generate an update statement that will update all the fields of the entity.

Can I use Efcore with .NET framework?

You can use EF Core in APIs and applications that require the full . NET Framework, as well as those that target only the cross-platform .


1 Answers

Unless there is a hidden exception that is hiding behind this as a dumb random exception, the reason is clearly stated in the exception.

Check the Id on the role object as you receive it on your Edit action and try to lookup that id in the database. The exception message you see states that, it is expecting to find a row with a matching Id of the object you attached, but it is not, so it is failing to do the update, since it could not locate a matching row to update it.

EDIT :

You are attaching the entity twice, remove the call to .Attach(role) and keep the line below it which is sufficient to add the object to the tracking context in a modified state.

//_db.Roles.Attach(role); //REMOVE THIS LINE !. _db.Entry(role).State = Microsoft.EntityFrameworkCore.EntityState.Modified; 

Beware that setting the state of the entry to modified will update all the property values upon calling .SaveChanges(), so in case you want to update only certain properties refer to this answer.

If this doesn't solve your problem, please check for any inner exceptions that you might've missed. Sometimes the exception messages don't make sense and mask the real problem which you might be able to find in the inner exception.

like image 127
Siraj Mansour Avatar answered Sep 20 '22 18:09

Siraj Mansour