我有以下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
答案 0 :(得分: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
方法中找到。