2015-08-19 46 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; 
相关问题