Concurrency Check attribute EF Core is used to handle conflicts that result when multiple users are updating (or deleting) the table at the same time. You can add the ConcurrencyCheck attribute on any property, which you want to participate in the Concurrency Check.
Table of Contents
What is Concurrency check
Assume that two users simultaneously query for same data to edit from the Employee
Table. One of the users saves his changes. Now, the other user is now looking at the data, which is invalid. If he also modifies the data and saves it, it will overwrite the first user’s changes. What if both users save the data at the same time. We never know which data gets saved.
We use the Concurrency check precisely to avoid such situations. To do that we include additional fields in the where clause apart from the primary key. For Example, by including the name field in the where clause, we are ensuring that the value in the name field has not changed since we last queried it. If someone has changed the field, then the where clause fails the Entity Framework raises the exception
ConcurrencyCheck Attribute
This attribute resides in the System.ComponentModel.DataAnnotations namespace
1 2 3 4 5 6 7 8 9 | public class Employee { public int EmployeeID { get; set; } [ConcurrencyCheck] public string Name { get; set; } public string Address { get; set; } } |
In the above example, We decorate the Name
Property with the ConcurrencyCheck
attribute. When Entity Name
column in where clause.
This attribute does not affect the database mapping in any way. This attribute is similar to timestamp attribute.
- You can apply
ConcurrencyCheck
Attribute on any number of properties. - There is no restriction on a data type for this attribute