I am 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'm trying to add user roles. I can add user roles, but I can’t 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 name of the new role is not stored in 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. 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. 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 Microsoft.AspNet.Identity.EntityFramework dependency instead of EntityFrameworkCore ) to edit database records using EF 7, Identity 3, etc.
Any thoughts on why this code does not allow changing records in the database?
source share