[英]Round trip transformation of ViewModel property in ASP.NET MVC
I'm working on an ASP.NET MVC application and I'm trying to figure out how to support optimistic concurrency. 我正在研究ASP.NET MVC应用程序,并且试图弄清楚如何支持开放式并发。 The approach I'm working through right now is to have the web app retrieve an instance of an entity (just a POCO), and build a ViewModel from it.
我现在正在研究的方法是让Web应用程序检索一个实体的实例(只是POCO),并从中创建一个ViewModel。 The ViewModel will include an
object UnderlyingEntity
property that holds the instance of the entity itself. ViewModel将包含一个
object UnderlyingEntity
属性,该属性包含实体本身的实例。 I can put the UnderlyingEntity
property in a hidden field in the view, then when the view is submitted, apply the changes to the UnderlyingEntity
, and send it off to be updated. 我可以将
UnderlyingEntity
属性放在视图的隐藏字段中,然后在提交视图时,将更改应用于UnderlyingEntity
,然后将其发送以进行更新。 Since I've persisted my original entity instance in the hidden field, when I save I have the original timestamp to be used for the concurrency check. 由于我将原始实体实例保留在隐藏字段中,因此在保存时,我具有用于并发检查的原始时间戳。
The hurdles I'm trying to overcome with this are: 我要克服的障碍是:
UnderlyingEntity
property. UnderlyingEntity
属性的往返序列化/反序列化。 I could serialize it easily enough in the view but how do I get it back to the original type when the form is posted? Nope. 不。 This is not how to do deal with concurrency in EF.
这不是在EF中处理并发的方法。
The best practice is the following steps: 最佳做法是以下步骤:
DbUpdateConcurrencyException
, catch that exception. DbUpdateConcurrencyException
,捕获该异常。 In the catch block, you do what you want. To learn more about that go to this link => https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application 要了解有关此内容的更多信息,请转到此链接=> https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency -with最实体框架功能于一个-ASP净MVC-应用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.