实体管理器 - 更新模型的参考ID

时间:2012-07-13 23:48:37

标签: c# asp.net-mvc-3 entity-framework updatemodel

我有一个Tablo对象,它引用了Ressam个对象。在Tablo的“编辑”操作中,我希望能够更改Ressam引用,即引用另一个RessamId。这是控制器代码,假设我只想更改通话中Ressam的{​​{1}}:

Tablo

顺便说一下,这不起作用。如何更新 [HttpPost] public ActionResult EditTablo(Tablo tablo, int? RessamId, HttpPostedFileBase image) { // Here, I successfully get RessamId, no problem there if (ModelState.IsValid) { // this is where I attach the Tablo object if (tablo is TuvalBaski) { container.Urun.Attach((TuvalBaski)tablo); } else if (tablo is YagliBoya) { container.Urun.Attach((YagliBoya)tablo); } // and this is the part where I change the Ressam reference if (RessamId == null) { tablo.Ressam = null; container.Ressam.Attach(tablo.Ressam); TryUpdateModel(tablo.Ressam); } else { tablo.Ressam = (from table in container.Ressam where table.RessamId == RessamId select table).Single(); //container.Ressam.Context.ObjectStateManager.ChangeObjectState(tablo.Ressam, System.Data.EntityState.Modified); //container.ObjectStateManager.ChangeObjectState(tablo.Ressam, System.Data.EntityState.Modified); container.Ressam.Attach(tablo.Ressam); TryUpdateModel(tablo.Ressam); } return View(tablo); } 实体中的参考ID,以便它可以显示另一个Tablo

2 个答案:

答案 0 :(得分:1)

您必须将tablo实例附加到上下文。

[HttpPost]
public ActionResult EditTablo(Tablo tablo, int? RessamId, HttpPostedFileBase image)
{ 
    if (ModelState.IsValid)
    {
        container.Tablo.Attach(tablo);
        container.ObjectStateManager
            .ChangeObjectState(tablo, System.Data.EntityState.Modified);

        if (RessamId != null)
        {
            tablo.Ressam = (from table in container.Ressam
                            where table.RessamId == RessamId
                            select table).Single();

            TryUpdateModel(tablo.Ressam);
        }

        container.SaveChanges();
    }

    return View(tablo);
}

答案 1 :(得分:0)

没有太多话题,这是完成工作的代码:

       [HttpPost]
        public ActionResult EditTablo(Tablo tablo, int? RessamId, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (tablo is TuvalBaski)
                {
                    container.Urun.Attach((TuvalBaski)tablo);
                }
                else if (tablo is YagliBoya)
                {
                    container.Urun.Attach((YagliBoya)tablo);
                }

                if (RessamId == null)
                {
                    if(tablo.Ressam != null)
                    {
                        container.Ressam.Detach(tablo.Ressam);
                    }

                    tablo.Ressam = null;
                }
                else
                {
                    if (tablo.Ressam != null)
                    {
                        container.Ressam.Detach(tablo.Ressam);
                    }

                    tablo.Ressam = (from table in container.Ressam
                                    where table.RessamId == RessamId
                                    select table).Single();


                    container.Ressam.Attach(tablo.Ressam);
                }

                TryUpdateModel(tablo);
                container.SaveChanges();
            }

            return View(tablo);
        }