• Skip to content
  • Skip to primary sidebar
  • Home
  • Angular
  • ASP.NET Core
  • Entity Framework
    • Entity Framework 6
    • Entity Framework Core
  • Crystal Reports
  • C#
  • ASP.NET
  • About Us
    • Privacy Policy
    • Contact Us

TekTutorialsHub

Free Online Tutorials

Data Annotations ConcurrencyCheck Attribute in EF 6 & EF Core

January 17, 2016 by TekTutorialsHub Leave a Comment

Complextype attribute
Timestamp attribute
 

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 Framework Code first generates the update or delete statement, it includes the Name column in where clause.

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
Complextype attribute
Timestamp attribute
 

Filed Under: Entity Framework Tagged With: Data Annotations

Leave a Reply

wpdiscuz_captcharefresh
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
wpdiscuz_captcharefresh
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of

Primary Sidebar

Copyright ©2008-2018

About us Contact Privacy Policy

Feb,22,2019 01:37:35 PM

Copyright © 2019 · Magazine Pro on Genesis Framework · WordPress · Log in

wpDiscuz
Our web site uses cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more