There are a number of ways in which we can implement optimistic concurrency using Entity Framework. In this section, we will take a look at a few of them.
Let's use a simple database to test the concurrency issues. Let's have a single Employee
table for our testing purpose. The Employee
table will contain only three fields: ID
, EmployeeName
, and Salary
. The generated Entity Data Model for this database will look like the following:
Let's now try to see how we can implement optimistic concurrency using Entity Framework.
Before we do anything explicitly, let's see how Entity Framework handles concurrency by default. Let's say we have our application designed to update the Salary
value of an employee. Let's implement a function to retrieve the values for the employee from the database and another to update the Employee
data in the database:
// Lets fetch...