2015-08-19 47 views
0

我正在使用Visual Studio 2015和MVC5。嘗試使用組合鍵更新表時出現DbUpdateConcurrencyException

我嘗試建立一個編輯視圖到表與此代碼,我也得到當由控制器接收到的數據是我不能解決

 @Html.HiddenFor(model => model.GHE_CORRELATIVO)*@ 

    <div class="form-group"> 
     @Html.LabelFor(model=> model.COP_CORRELATIVO,"COP_CORRELATIVO") 
     @Html.DropDownList("COP_CORRELATIVO", null, htmlAttributes: new { @class = "form-control" }) 

    </div> 

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

DbUpdateConcurrencyExcepcion一個錯誤。

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Edit([Bind(Include = "COP_CORRELATIVO,GHE_CORRELATIVO,GHE_DESCRIPCION")] GRUPO_HERRAMIENTA gRUPO_HERRAMIENTA) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Entry(gRUPO_HERRAMIENTA).State = EntityState.Modified; 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    ViewBag.COP_CORRELATIVO = new SelectList(db.CLASIFICACION_OPCION, "COP_CORRELATIVO", "COP_DESCRIPCION", gRUPO_HERRAMIENTA.COP_CORRELATIVO); 
    return View(gRUPO_HERRAMIENTA); 
} 

我的表是由兩個PK,ghe_correlativo(自己)和cop_correlativo(FK)組成,支架僅生成文本輸入,但我需要編輯cop_correlativo場下拉FIEL。

模型的結構:

public partial class GRUPO_HERRAMIENTA 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public GRUPO_HERRAMIENTA() 
     { 
      this.PERFIL_INTRANET = new HashSet<PERFIL_INTRANET>(); 
     } 

     public int COP_CORRELATIVO { get; set; } 
     public int GHE_CORRELATIVO { get; set; } 
     public string GHE_DESCRIPCION { get; set; } 

     public virtual CLASIFICACION_OPCION CLASIFICACION_OPCION { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<PERFIL_INTRANET> PERFIL_INTRANET { get; set; } 
    } 

我究竟做錯了什麼?

+0

請提供您的實體的結構。看起來你在視圖中缺少一個rowversion屬性。 –

+0

嗨@FabioLuz,我剛剛添加了結構,謝謝! –

+0

您確定您的模型沒有行版本列嗎?看看數據庫中的表格; GRUPO_HERRAMIENTA是一個部分類。你確定原班上沒有其他物業嗎? –

回答

0

在調試中,確保所有參數都在後置動作中設置,特別是COP_CORRELATIVO,這是主鍵。

爲了測試puposes,而不是db.Entry(gRUPO_HERRAMIENTA).State = EntityState.Modified;使用本:

db.GRUPO_HERRAMIENTAS.Attach(gRUPO_HERRAMIENTA); 

db.Entry(gRUPO_HERRAMIENTA).Property(i => i.COP_CORRELATIVO).IsModified = true; 
db.Entry(gRUPO_HERRAMIENTA).Property(i => i.GHE_CORRELATIVO).IsModified = true; 
db.Entry(gRUPO_HERRAMIENTA).Property(i => i.GHE_DESCRIPCION).IsModified = true; 
相關問題