我正在開發一個ASP.Net MVC 3 Web應用程序使用實體框架4.1,但是,我有更新實體的導航實體的問題。實體框架更新導航實體
我有一個實體,稱爲移
public partial class Shift
{
public Shift()
{
this.Locations = new HashSet<ShiftLocation>();
}
public int shiftID { get; set; }
public string shiftTitle { get; set; }
public virtual ICollection<ShiftLocation> Locations { get; set; }
}
在我的換檔控制器,我有兩個HttpPost方法來創建和編輯的轉變。
[HttpPost]
public ActionResult CreateShift(ViewModelShift model)
{
if (ModelState.IsValid)
{
Shift shift = new Shift();
shift = Mapper.Map<ViewModelShift, Shift>(model);
//Create new shift location and add it to the newly created Shift
ShiftLocation location = new ShiftLocation();
location.locationID = model.locationID;
shift.Locations.Add(location);
_shiftService.AddShift(shift);
return RedirectToAction("Index");
}
}
[HttpPost]
public ActionResult EditShift(ViewModelShift model)
{
if (ModelState.IsValid)
{
Shift shift = new Shift();
shift = Mapper.Map<ViewModelShift, Shift>(model);
ShiftLocation location = new ShiftLocation();
//location = Mapper.Map<ViewModelShift, ShiftLocation>(model);
location.shiftID = model.shiftID;
location.locationID = model.locationID;
shift.Locations.Add(location);
_shiftService.UpdateShift(shift);
return RedirectToAction("Index");
}
}
的CreateShift法正常工作,即插入一條記錄到數據庫中的一個新的轉變,也爲另一個表內的移位位置上的新紀錄。但是,EditShift方法僅更新Shift記錄,但它不會更新Shift位置記錄,即使我已分配了shiftID和新的locationID。
關於如何解決這個問題的任何信息將不勝感激。
謝謝。
編輯
我ShiftLocation類是如下
public partial class ShiftLocation
{
public int shiftLocationID { get; set; }
public int shiftID { get; set; }
public int locationID { get; set; }
public virtual Shift Shift { get; set; }
}
的ShiftService類中的UpdateShift方法如下
public void UpdateShift(Shift item)
{
_UoW.Shifts.Update(item);
_UoW.Commit();
}
這然後調用Update方法我通用庫
public void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
第二編輯
從Slauma的回答繼我編輯EditShift POST操作以下
if (ModelState.IsValid)
{
//Shift shift = new Shift();
Shift shift = _shiftService.GetShiftByID(model.shiftID);
ShiftLocation shiftLocation = shift.Locations.Where(s => s.shiftID == model.shiftID).Single();
shift = Mapper.Map<ViewModelShift, Shift>(model);
shiftLocation.locationID = model.locationID;
shift.Locations.Add(shiftLocation);
_shiftService.UpdateShift(shift);
return RedirectToAction("Index");
}
我現在的問題是,當我在通用存儲庫更新方法被調用,以下發生錯誤
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same key
現在,我現在爲什麼會發生這種情況,因爲在我的EditPost操作中,我檢索Shift的一個實例,然後在通用庫中的更新方法中,我試圖附加相同的Shift。
我現在只是很困惑。 My Generic Repositro看起來不錯,因爲我從本文頂部提到的Microsoft MVC教程中複製了它。
再次有關如何獲得這個簡單的更新工作的任何幫助將不勝感激。
您可以顯示'ShiftLocation'類和'_shiftService.UpdateShift(移)'裏面的代碼? – Slauma 2012-04-03 18:11:46
@Slauma - 請參閱我的編輯。謝謝。 – tgriffiths 2012-04-03 19:56:36
你想要達到什麼目的?想象一下,「shift」存儲在數據庫中,並且在DB中已經有5個「ShiftLocations」分配給它。現在你到達'EditShift'後期操作。在你的代碼中,你創建一個'ShiftLocation'對象並將它添加到'shift.Locations'集合中。您現在需要:1)將這個新的位置添加到已有的位置,或者2)您想刪除現有的5個位置並將其替換爲新的位置,或者3)您是否希望找到位置爲其中一個(通過Id?),然後用新值更新它? – Slauma 2012-04-03 20:49:24