0
我有庫類內部下面的方法,來檢索Session
對象和相關聯的Medicine object
(通過定義使用.Include
預先加載),如下所示: -預先加載不包括來自我的庫類的導航屬性
public Session GetSession(int id)
{
return entities.Sessions.Include(d => d.Medicine).FirstOrDefault(d => d.SessionID == id);
}
我的行動方法,調用上述庫方法看起來如下: -
[HttpPost]
public ActionResult Delete(int id)
{
try
{
//string desc;
var s = repository.GetSession(id);
repository.DeleteSession(s);
repository.Save();
return Json(new { IsSuccess = "True", id = s.SessionID, description = s.Medicine.Name }, JsonRequestBehavior.AllowGet);
}
catch (ArgumentNullException)
//code goes here
我面臨的問題是,經過物理刪除使用對象,,我將無法從內存中訪問Medicine navigation property
,並且會引發以下異常NullReferenceException未被用戶代碼對description = s.Medicine.Name
處理,而我可以訪問s.SessionID
,即使在刪除對象,這是否意味着Session
被刪除的對象沒有Include
(沒有急切加載)Medicine
導航屬性!!!? BR
感謝您的回覆,在會話表(medicineId)中有一個外鍵,它引用了藥物表的PK,它是drugID。 –