我有以下操作方法:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Rack rack)
{
try
{
if (ModelState.IsValid)
{
repository.InsertOrUpdateRack(rack);
repository.Save();
return RedirectToAction("Index");
}
}
catch (DbUpdateConcurrencyException ex)
{
var entry = ex.Entries.Single();
var databaseValues = (Rack)entry.GetDatabaseValues().ToObject();
var clientValues = (Rack)entry.Entity;
if (databaseValues.RU != clientValues.RU)
ModelState.AddModelError("RU", "Current value: "
+ databaseValues.RU);
if (databaseValues.DataCenterID != clientValues.DataCenterID)
ModelState.AddModelError("DataCenterID", "Current value: "
+ databaseValues.DataCenter.Name);
if (databaseValues.ZoneID != clientValues.ZoneID)
ModelState.AddModelError("ZoneID", "Current value: "
+ databaseValues.Zone.Name);
但是,如果引发 DbUpdateConcurrncy 异常,我将在 databaseValues.DataCenter.Name
&上获得 Null 引用异常databaseValues.Zone.Name
。看来我无法从 . 访问两个导航属性(数据中心和机架)Getdatabase().Toobject
。知道如何解决这个问题吗?