2012-07-13 45 views
0

我有一個Tablo對象,該對象引用了Ressam對象。在我的Tablo的編輯操作中,我希望能夠更改Ressam參考,即引用另一個RessamId。這裏的控制器代碼,讓我們說我只是想改變TabloRessam在我的電話:實體管理器 - 更新我的模型的參考ID

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

順便說一句,這是行不通的。我如何更新我的Tablo實體中的參考ID,以便它可以顯示另一個Ressam

回答

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); 
     } 
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); 
} 
+0

讓我編輯的問題,然後 – Halo 2012-07-14 09:19:56

+0

這並沒有通過工作的方式,應用程序堅持零的行數分別爲更新 – Halo 2012-07-14 09:32:43

+0

@Halo如果'ModelState.IsValid'然後至少一行應該更新。確保所有操作只使用一個ObjectContext實例。 – Eranga 2012-07-14 11:22:21