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 the where clause.

using System.ComponentModel.DataAnnotations;

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

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

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

    context.Students.Add(std);
    context.SaveChanges();

    std.StudentName = "Steve";
    context.SaveChanges();
}

The above example will execute the following UPDATE statement on SaveChanges(), where it includes 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'
go            

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