简体   繁体   English

尝试使用组合键更新表时出现DbUpdateConcurrencyException

[英]DbUpdateConcurrencyException when try update a table with composed key

Im using Visual Studio 2015 with MVC5. 我将Visual Studio 2015与MVC5一起使用。

I try build a edit view to a table with this code, and i get a error that I can´t solve 我尝试使用此代码在表格中建立编辑视图,但出现一个我无法解决的错误

        @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 when the data is received by the controller. 控制器接收数据时的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);
}

My table is composed by two pk, ghe_correlativo (own) and cop_correlativo (FK), the scaffold only generate the text input but I need editing the cop_correlativo field with a dropdown fiel. 我的表由两个pk组成,分别是ghe_correlativo(所有者)和cop_correlativo(FK),该支架仅生成文本输入,但是我需要使用下拉字段编辑cop_correlativo字段。

The structure of model : 模型结构:

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

what am I doing wrong? 我究竟做错了什么?

In debug, make sure that all parameters are being set in the post action, specially COP_CORRELATIVO , which is the primary key. 在调试中,请确保在后操作中设置了所有参数,尤其是主键COP_CORRELATIVO

For testing puposes, instead of db.Entry(gRUPO_HERRAMIENTA).State = EntityState.Modified; 用于测试目的,而不是db.Entry(gRUPO_HERRAMIENTA).State = EntityState.Modified; use this: 用这个:

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;

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

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