0

我有以下 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
4

1 回答 1

1

我不完全清楚这部分代码中发生了什么,您是否打算Resource从您在评论中提到的第二个上下文中检索?

 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;

如果你调用Single()一个包含多个元素的序列,你会产生一个异常;由于Single不会产生异常,因此您只存在一个在两种情况下都使用的条目。

首先,您想将其视为Rack,这似乎不会产生投诉。在第二个中,您将其视为Resource,这似乎与Rack您的投诉原因不相容。

InvalidCastException: Unable to cast object of type 'System.Data.Entity.DynamicProxies.Rack_81...' to type 'TMS.Models.Resource'.]

您的异常仅包含该特定实体的原因很可能在您的InsertOrUpdateRack方法中找到。

于 2013-08-05T13:07:36.117 回答