Data Annotation - ConcurrencyCheck Attribute in EF 6 & EF Core

The ConcurrencyCheck attribute can be applied to one or more properties in an entity class in EF 6 and EF Core. When applied to a property, the corresponding column in the database table will be used in the optimistic concurrency check using where clause.

using System.ComponentModel.DataAnnotations;

public class Student
    public int StudentId { get; set; }
    public string StudentName { get; set; }

In the above example, ConcurrencyCheck attribute is applied to StudentName property of the Student entity class. So, EF will include StudentName column in UPDATE statement to check for optimistic concurrency. Consider the following example.

using(var context = new SchoolContext()) 
    var std = new Student()
        StudentName = "Bill"


    std.StudentName = "Steve";

The above example will execute the following UPDATE statement on SaveChanges() where it incudes StudentName in the where clause.

exec sp_executesql N'UPDATE [dbo].[Students]
SET [StudentName] = @0
WHERE (([StudentId] = @1) AND ([StudentName] = @2))
',N'@0 nvarchar(max) ,@1 int,@2 nvarchar(max) ',@0=N'Steve',@1=1,@2=N'Bill'

Note: The Timestamp attribute can only be applied to a single byte array property, whereas ConcurrencyCheck attribute can be applied to any number of properties with any data type.

Test Your Entity Framework Knowledge: