MVC核心错误-> DbUpdateConcurrencyException:数据库操作预期会影响1行,但实际上会影响0行

时间:2020-07-18 13:06:02

标签: c# model-view-controller core identity

我知道有几个关于此错误的问题,但没有一个对我有帮助! 我正在使用MVC Core和Identiy,并且正在尝试更新角色。但这有点令人不安,因为我找不到我的代码有什么问题。

错误如下:

DbUpdateConcurrencyException:数据库操作预期影响1行,但实际上影响0行。自加载实体以来,数据可能已被修改或删除。有关了解和处理乐观并发异常的信息,请参见http://go.microsoft.com/fwlink/?LinkId=527962

screen of the error is display here

我的控制器:

public async Task<IActionResult> Edit(string id)
{

    if (id == null)
    {
        return NotFound();
    }

    //var role = await roleManager.FindByIdAsync(id);
    var role = await roleManager.Roles.FirstOrDefaultAsync(f => f.Id == id);

    if (role == null)
    {
        return NotFound();
    }
    return View(role);

}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(string id, [Bind("Id,Name")] IdentityRole role)
{
    if (id != role.Id)
    {
        return NotFound();
    }

    if (ModelState.IsValid)
    {

        try
        {
            _context.Entry(role).State = EntityState.Modified;
            await roleManager.UpdateAsync(role);
            await _context.SaveChangesAsync();

            return RedirectToAction(nameof(Index));

        }
        catch (DbConcurrencyException ex)
        {

            throw new DbConcurrencyException(ex.Message);
        }


    }
    return View(role);
}

我的视图:

    @model Microsoft.AspNetCore.Identity.IdentityRole

@{
    ViewData["Title"] = "Edit";
}

<h1>Edit</h1>

<h4>Roles</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="@Model.Id" />
            <div class="form-group">
                <label asp-for="@Model.Name" class="control-label"></label>
                <input asp-for="@Model.Name" class="form-control" />
                <span asp-validation-for="@Model.Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-primary" />
            </div>
       
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

如果能解决此错误,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的代码不正确。替换为:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit([Bind("Id,Name")] IdentityRole role)
        {
            if (ModelState.IsValid)
            {
                var roleItem = await _roleManager.FindByIdAsync(role.Id);
                if (roleItem != null)
                {
                    roleItem.Name = role.Name;

                    var updateResult = await _roleManager.UpdateAsync(roleItem);
                    if (updateResult.Succeeded)
                    {
                        return RedirectToAction(nameof(Index));
                    }
                    else
                    {
                        //Handle errors
                    }
                }
                else
                {
                    //Handle errors
                }
            }
            return View(role);
        }