我有以下操作方法:-
[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 & databaseValues.Zone.Name
. 似乎我无法从Getdatabase().Toobject
.
知道如何解决这个问题吗?请记住,这两个导航值已经在数据库中具有值。