I'm working on an ASP.NET MVC application and I'm trying to figure out how to support optimistic concurrency. 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. The ViewModel will include an object UnderlyingEntity
property that holds the instance of the entity itself. 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. 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. 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? I think I need a ModelBinder or ValueProvider but I'm not sure exactly where to start. Nope. This is not how to do deal with concurrency in EF.
The best practice is the following steps:
DbUpdateConcurrencyException
, catch that exception. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.