用户代码

时间:2017-02-28 17:52:53

标签: c# visual-studio asp.net-mvc-5

  

类型的例外   'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException'   发生在EntityFramework.dll中但未在用户代码中处理

     

其他信息:存储更新,插入或删除语句   影响了意外的行数(0)。实体可能已经存在   修改或删除

您好我正在使用代码第一个数据库实现,每当我尝试编辑我的文档引用时,我都会收到上述错误。代码如下。

控制器编辑方法

[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);
}

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; }
    }
}

视图

@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")
}

1 个答案:

答案 0 :(得分:0)

嗨,我知道这可能不是其他人正在寻找的答案,但我只是通过重新设计我的项目并复制代码来解决我自己的问题所以我假设自动生成的属性导致了我的问题。谢谢所有试图帮助我的人。