以下に示すように、リポジトリクラス内に次のメソッドがあり、Session
オブジェクトと関連するものを取得しますMedicine object
(を使用してEagerの読み込みを定義することにより.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
私が直面している問題は、を使用してオブジェクトを物理的に削除した後 、メモリからrepository.Save();
にアクセスできなくなり、次の例外が発生することです。NullReferenceExceptionは、のユーザーコードによって処理されませんでしたが、使用可能なにアクセスできます。オブジェクトを削除した後でもメモリ内にあるので、これは、削除されたオブジェクトがナビゲーションプロパティを(熱心にロードしなかった)しなかったことを意味します!!!?BRMedicine navigation property
description = s.Medicine.Name
s.SessionID
Session
Include
Medicine