C# – Handle concurrency – ASP.NET MVC with EF Core

After updating the related data in the ASP.NET tutorial, it is time for the next step – handling concurrency in the MVC Application. These are the points, followed by the video:

  • Learn about concurrency conflicts
  • Add a tracking property
  • Create Departments controller and views
  • Update Index view
  • Update Edit methods
  • Update Edit view
  • Test concurrency conflicts
  • Update the Delete page
  • Update Details and Create views

So, let’s imagine we have two users – Peter and George. They both want to edit the same department and they open the “Edit” link in the same time. Peter is faster, thus he clicks “Save” first and his edits are written to the database. However, when George clicks “Save”, after making his edits, the edits are not written in the database. George is informed, that the database entry was changed after he has decided to edit the entry, thus if his entry is written down, he may overwrite an entry by someone else. The current values are also written down in red:

This is the Edit HttpPost method, that carries out this action, comparing the “old” entry with the new one:

The rest of the code is available in my GitHub account – https://github.com/Vitosh/ASP/tree/master/EFCoreAsp.NetMvcWebApp/ContosoUniversity008

Tagged with: , , , ,