2017-02-28 31 views
1

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' occurred in EntityFramework.dll but was not handled in user codeDBUpdateConcurrencyException被用戶代碼未處理

Additional information: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted

嗨我正在使用代碼第一個數據庫實現,並且每當我試圖編輯我的文檔引用我得到上述錯誤。下面的代碼。

控制器編輯方法

[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

在這裏檢查有關此錯誤:http://stackoverflow.com/questions/32498546/dbconcurrency-exception-occured-while-updating-using-dataadapter –

+0

我看到了這個答案,但不知道如何真正解決它這個問題在我的情況下 – Kezanub

+0

檢查'RevisionNumber'是否是表中的一個可爲空的int字段,添加空白或dupe值的記錄會引發錯誤(在SSMS中設置'IGNORE_DUP_KEY = OFF'來處理它)。如果它仍然不起作用,請嘗試在'Bind'上包含PK字段:'public ActionResult Edit([Bind(Include =「DocumentID,DocTitle,RevisionNumber」,Exclude =「DocumentAuthor,CreationDate,DocumentStatus」)] ..}' –

回答

1

您好我知道這可能不是別人正在尋找的答案,但我只是固定的我自己的問題通過重建我的項目和複製代碼,所以我假設的自動生成的屬性導致我的概率LEM。感謝所有試圖幫助我的人。

相關問題