我有以下 POST 编辑操作方法:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(RackJoin rj,FormCollection formValues)
{
try
{
if (ModelState.IsValid)
{
repository.InsertOrUpdateRack(rj.Rack, User.Identity.Name, rj.Resource.RESOURCEID);
repository.Save();
return RedirectToAction("Index");
}
}
catch (DbUpdateConcurrencyException ex)
{
var entry = ex.Entries.Single();
var databaseValues = (Rack)entry.GetDatabaseValues().ToObject();
var clientValues = (Rack)entry.Entity;
var entry2 = ex.Entries.Single();
var databaseValues2 = (Resource)entry2.GetDatabaseValues().ToObject();
var clientValues2 = (Resource)entry2.Entity;
if (databaseValues.RU != clientValues.RU)
ModelState.AddModelError("Rack.RU", "Current value: "
+ databaseValues.RU);
但是当引发 DbUpdateConcurrencyException 异常时,我在以下代码行中收到以下异常var databaseValues2 = (Resource)entry2.GetDatabaseValues().ToObject();
:-
Unable to cast object of type 'System.Data.Entity.DynamicProxies.Rack_81556130B3DB7E3D4F63B9FF3F15832A81A86055EED840211E95A71E1342027D' to type 'TMS.Models.Resource'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Data.Entity.DynamicProxies.Rack_81556130B3DB7E3D4F63B9FF3F15832A81A86055EED840211E95A71E1342027D' to type 'TMS.Models.Resource'.
Source Error:
Line 175: var clientValues = (Rack)entry.Entity;
Line 176: var entry2 = ex.Entries.Single();
Line 177: var databaseValues2 = (Resource)entry2.GetDatabaseValues().ToObject();
Line 178: var clientValues2 = (Resource)entry2.Entity;
Line 179:
Source File: c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TMS\TMS\Controllers\RackController.cs Line: 177
Stack Trace:
[InvalidCastException: Unable to cast object of type 'System.Data.Entity.DynamicProxies.Rack_81556130B3DB7E3D4F63B9FF3F15832A81A86055EED840211E95A71E1342027D' to type 'TMS.Models.Resource'.]
TMS.Controllers.RackController.Edit(RackJoin rj, FormCollection formValues) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TMS\TMS\Controllers\RackController.cs:177
lambda_method(Closure , ControllerBase , Object[] ) +245
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +50
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +75
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +139