ConcurrencyCheck Attribute
Concurrency Check used to handle conflicts that results 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. This attribute works the same way in both EF 6 & EF Core
What is Concurrency check
Assume that two users simultaneously query the same data to edit from the Employee Table. If the first user updates the data it gets saved first. Now the second user is now looking at the data, which is already changed and invalid. Now if the second user also modifies the data it gets saved overwriting the first user’s changes. What if both users save the data at the same time. We never know which data gets saved.
The Concurrency check used to precisely for the same reason. We include the fields in the where clause. 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
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, Name Property is decorated with the ConcurrencyCheck attribute. The Entity
This attribute does not affect the database mapping in any way. This attribute is similar to timestamp attribute.
- ConcurrencyCheck Attribute can be applied on any Number of property in Domain model
- There is no restriction on data type for this attribute
Leave a Reply