无法使用EFCore,EntityState编辑数据库条目.修改:“数据库操作预期会影响1行,但实际上影响0行." [英] Unable to edit db entries using EFCore, EntityState.Modified: "Database operation expected to affect 1 row(s) but actually affected 0 row(s)."

查看:758
本文介绍了无法使用EFCore,EntityState编辑数据库条目.修改:“数据库操作预期会影响1行,但实际上影响0行."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Identity Core 1.0与ASP.NET MVC Core 1.0和Entity Framework Core 1.0一起使用

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" />
}

新角色名称未保存到数据库,并且出现以下异常: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.

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.

我能够使用精确的代码(具有Microsoft.AspNet.Identity.EntityFramework依赖性而​​不是EntityFrameworkCore)来使用EF 7,Identity 3等编辑数据库条目.

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?

推荐答案

除非隐藏的异常将其隐藏为愚蠢的随机异常,否则在异常中明确说明原因.

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

Edit操作中收到role对象上的Id并对其进行检查,然后尝试在数据库中查找该ID.您看到的异常消息指出,它期望找到与您附加的对象的ID匹配的行,但是没有,因此它无法执行更新,因为它无法找到匹配的行来更新它

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.

您要附加两次实体,删除对.Attach(role)的调用,并保留在其下方的行,这足以将对象以修改后的状态添加到跟踪上下文中.

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;

请注意,将条目的状态设置为Modifyed将在调用.SaveChanges()时更新所有属性值,因此,如果只想更新某些属性,请参考

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.

这篇关于无法使用EFCore,EntityState编辑数据库条目.修改:“数据库操作预期会影响1行,但实际上影响0行."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆