Rahul Rai
Rahul Rai

Reputation: 370

why we use Concurrency Check Attribute in Entity Framework

I am new to entity framework.Can any one explain why we use Concurrency Check attribute in entity framework with example.In scenario we use this attribute.

Thanks in advance..

Upvotes: 14

Views: 14896

Answers (1)

Law
Law

Reputation: 369

It's a way to handle conflicts to database changes when multiple users are updating entities at the same time. Adding the ConcurrencyCheck attribute means that you are telling Entity Framework to use that property for detecting concurrency conflicts. Entity Framework includes the property in UPDATEs or DELETEs to the database.

For database tables that have many columns this can mean very large WHERE clauses, which can affect performance and require you to manage large amounts of state. For larger databases a row version strategy is preferred.

[Table("Accounts"]
public class Account
{
    public Account() {}
    [Key]
    public int AccountID { get; set; }
    [ConcurrencyCheck]
    public string AccountName { get; set; } 
}

SQL Server will include AccountName in UPDATEs or DELETEs to the database:

exec sp_executesql N'UPDATE [dbo].[Accounts]
SET [AccountName] = @0
WHERE (([AccountId] = @1) AND ([AccountName] = @2))
',N'@0 nvarchar(max) ,@1 int,@2 nvarchar(max) ',@0=N'Dick',@1=1,@2=N'Harry'
go

This returns no (zero) rows to the user because of the concurrency check.

Upvotes: 16

Related Questions