简体   繁体   English

用户代码未处理DBUpdateConcurrencyException

[英]DBUpdateConcurrencyException was unhandled by user code

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型为'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException'的异常,但未在用户代码中处理

Additional information: Store update, insert, or delete statement affected an unexpected number of rows (0). 附加信息:存储更新,插入或删除语句影响了意外的行数(0)。 Entities may have been modified or deleted 实体可能已被修改或删除

Hi I am using code first database implementation and whenever I am trying to edit my document references I get the above error. 嗨,我使用代码优先的数据库实现,每当我尝试编辑文档引用时,都会出现上述错误。 Code below. 下面的代码。

Controller Edit Method 控制器编辑方法

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "DocTitle,RevisionNumber", Exclude = "DocumentID,DocumentAuthor,CreationDate,DocumentStatus")] Document document)
{
    if (ModelState.IsValid)
    {
        db.Entry(document).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(document);
}

Class

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using System.Linq;
using System.Web;

namespace IP3Latest.Models
{
    public class Document
    {
        [Key]
        [ScaffoldColumn(false)]
        public int DocumentID { get; set; }

       [Required]
       public string DocTitle { get; set; }

       [Required]
       [Index("IX_RevisionNumberUnique", 1, IsUnique = true)]
       public int RevisionNumber { get; set; }

       [ScaffoldColumn(false)]
       public string DocumentAuthor { get; set; } 

       [ScaffoldColumn(false)]
       public DateTime CreationDate { get; set; }

       [ScaffoldColumn(false)]
       public string FilePath { get; set; }

       [ScaffoldColumn(false)]
       public string DocumentStatus { get; set; }
    }
}

view 视图

@model IP3Latest.Models.Document

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Document</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.DocTitle, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DocTitle, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DocTitle, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RevisionNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RevisionNumber, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.RevisionNumber, "", new { @class = "text-danger" })
            </div>
        </div>

        @Html.HiddenFor(model => model.DocumentID)
        @Html.HiddenFor(model => model.DocumentAuthor)
        @Html.HiddenFor(model => model.CreationDate)
        @Html.HiddenFor(model => model.FilePath)
        @Html.HiddenFor(model => model.DocumentStatus)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Hi I know this may not be the answer others are looking for but I fixed my own problem simply by remaking my project and copying the code over so i'm assuming on of the auto generated properties was causing my problem. 嗨,我知道这可能不是别人想要的答案,但我只是通过重新构建项目并复制代码来解决了自己的问题,所以我假设自动生成的属性中的一个导致了我的问题。 Thanks everyone who attempted to help me. 感谢所有试图帮助我的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM