Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 689
Rahul Rai Avatar asked May 25 '14 15:05

Rahul Rai


1 Answers

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.

like image 155
Law Avatar answered Oct 22 '22 13:10

Law