entity framework 不允许在数据库中保存数据
entity framework doesnt allow to save data in db
我在我的应用程序中使用 entityframework codefirst。
我有 2 tables:
1.Order
2.Orderdetails
我想从一个表单中收集数据并在一个操作中将其保存在这 2 table 中。
我的表单使用名为 "OrderViewModel".
的视图模型
我想在用户单击提交时将数据保存在 "Order" table 和 "Orderdetail" table 中。但我有打击错误:
The model item passed into the dictionary is of type
'Amooshahryar.Models.Order', but this dictionary requires a model item
of type 'Amooshahryar.Models.ViewModels.OrderViewModel'.
我的表单使用 'Amooshahryar.Models.ViewModels.OrderViewModel' 并且在 actionresult 中我想按顺序保存数据 table 使用 'Amooshahryar.Models.Order'。
我不明白是什么原因造成的问题。
这是我的代码:
public ActionResult آدرس_و_پرداخت(FormCollection values)
{
var order = new Order() {};
TryUpdateModel(order);
int totalcartprice = 0;
try
{
order.Username = User.Identity.Name;
order.OrderDate = DateTime.Now;
order.DeliveryStatusID = 1;
TryUpdateModel(order);
//Save Order
db.Orders.Add(order);
db.SaveChanges(); <===========here i get error
//Process the order
//And rest of the code that i havent write here
}
catch
{
//Invalid - redisplay with errors
return View(order);
}
}
感谢任何帮助...
谢谢你。
编辑:
这是我的堆栈跟踪:
[InvalidOperationException: The model item passed into the dictionary is of type 'Amooshahryar.Models.Order', but this dictionary requires a model item of type 'Amooshahryar.Models.ViewModels.OrderViewModel'.]
System.Web.Mvc.ViewDataDictionary1.SetModel(Object value) +378
System.Web.Mvc.ViewDataDictionary.set_Model(Object value) +47
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +614
System.Web.Mvc.ViewDataDictionary
1..ctor(ViewDataDictionary viewDataDictionary) +37
System.Web.Mvc.WebViewPage1.SetViewData(ViewDataDictionary viewData) +98
System.Web.Mvc.WebViewPage.set_ViewData(ViewDataDictionary value) +39
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +425
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1 filters, ActionResult actionResult) +106
System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +321
System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9748493
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
这是正在发生的事情:
1) 您的 db.SaveChanges 由于某种原因失败,并抛出异常。
2) 代码因此移动到 Catch 块。
3) 您忽略异常,并尝试执行 return 语句。不幸的是,您的 return 语句也失败了,因为您传递了错误类型的对象。然后这会产生您在屏幕上看到的异常,因为它没有被捕获。
不要像这样压缩异常。如果发生异常,那是因为发生了一些不寻常的事情并且实际上是错误的,所以应用程序崩溃是可以的。不要隐藏它,请始终记录它并进行调查。 Try-catch 用于仅捕获不可避免的异常。数据库崩溃不应该是不可避免的,它应该是不寻常的。此外,如果您在 catch 中的 return 语句确实成功,则用户不会收到有问题的反馈,并且会想知道为什么他们再次出现在同一屏幕上。与此同时,作为开发者,您不会注意到您的应用已损坏
我在我的应用程序中使用 entityframework codefirst。
我有 2 tables:
1.Order
2.Orderdetails
我想从一个表单中收集数据并在一个操作中将其保存在这 2 table 中。
我的表单使用名为 "OrderViewModel".
的视图模型
我想在用户单击提交时将数据保存在 "Order" table 和 "Orderdetail" table 中。但我有打击错误:
The model item passed into the dictionary is of type
'Amooshahryar.Models.Order', but this dictionary requires a model item of type 'Amooshahryar.Models.ViewModels.OrderViewModel'.
我的表单使用 'Amooshahryar.Models.ViewModels.OrderViewModel' 并且在 actionresult 中我想按顺序保存数据 table 使用 'Amooshahryar.Models.Order'。
我不明白是什么原因造成的问题。
这是我的代码:
public ActionResult آدرس_و_پرداخت(FormCollection values)
{
var order = new Order() {};
TryUpdateModel(order);
int totalcartprice = 0;
try
{
order.Username = User.Identity.Name;
order.OrderDate = DateTime.Now;
order.DeliveryStatusID = 1;
TryUpdateModel(order);
//Save Order
db.Orders.Add(order);
db.SaveChanges(); <===========here i get error
//Process the order
//And rest of the code that i havent write here
}
catch
{
//Invalid - redisplay with errors
return View(order);
}
}
感谢任何帮助...
谢谢你。
编辑:
这是我的堆栈跟踪:
[InvalidOperationException: The model item passed into the dictionary is of type 'Amooshahryar.Models.Order', but this dictionary requires a model item of type 'Amooshahryar.Models.ViewModels.OrderViewModel'.] System.Web.Mvc.ViewDataDictionary
1.SetModel(Object value) +378 System.Web.Mvc.ViewDataDictionary.set_Model(Object value) +47 System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +614 System.Web.Mvc.ViewDataDictionary
1..ctor(ViewDataDictionary viewDataDictionary) +37 System.Web.Mvc.WebViewPage1.SetViewData(ViewDataDictionary viewData) +98 System.Web.Mvc.WebViewPage.set_ViewData(ViewDataDictionary value) +39 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +425 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1 filters, ActionResult actionResult) +106 System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +321 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +185 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +39 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +62 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9748493 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
这是正在发生的事情:
1) 您的 db.SaveChanges 由于某种原因失败,并抛出异常。
2) 代码因此移动到 Catch 块。
3) 您忽略异常,并尝试执行 return 语句。不幸的是,您的 return 语句也失败了,因为您传递了错误类型的对象。然后这会产生您在屏幕上看到的异常,因为它没有被捕获。
不要像这样压缩异常。如果发生异常,那是因为发生了一些不寻常的事情并且实际上是错误的,所以应用程序崩溃是可以的。不要隐藏它,请始终记录它并进行调查。 Try-catch 用于仅捕获不可避免的异常。数据库崩溃不应该是不可避免的,它应该是不寻常的。此外,如果您在 catch 中的 return 语句确实成功,则用户不会收到有问题的反馈,并且会想知道为什么他们再次出现在同一屏幕上。与此同时,作为开发者,您不会注意到您的应用已损坏