0
我有一個Tablo
對象,該對象引用了Ressam
對象。在我的Tablo
的編輯操作中,我希望能夠更改Ressam
參考,即引用另一個RessamId
。這裏的控制器代碼,讓我們說我只是想改變Tablo
的Ressam
在我的電話:實體管理器 - 更新我的模型的參考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
?
讓我編輯的問題,然後 – Halo 2012-07-14 09:19:56
這並沒有通過工作的方式,應用程序堅持零的行數分別爲更新 – Halo 2012-07-14 09:32:43
@Halo如果'ModelState.IsValid'然後至少一行應該更新。確保所有操作只使用一個ObjectContext實例。 – Eranga 2012-07-14 11:22:21