无法使用 EFCore 编辑数据库条目,EntityState.Modified:“数据库操作预计会影响 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)."

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

问题描述

我将 Identity Core 1.0 与 ASP.NET MVC Core 1.0 和 Entity Framework Core 1.0 结合使用,以创建一个带有 这篇文章 作为起点,我正在尝试添加用户角色.我可以添加用户角色,但我无法编辑它们.这是 RolesController 中的 Edit 操作:

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

新角色名称未保存到数据库中,并且出现以下异常:数据库操作预计会影响 1 行,但实际上影响了 0 行.自加载实体以来,数据可能已被修改或删除.

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.

检查 role 对象上的 Id,因为您在 Edit 操作中收到它,并尝试在数据库中查找该 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;

请注意,将条目的状态设置为已修改将在调用 .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.Modified:“数据库操作预计会影响 1 行,但实际上影响了 0 行."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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