Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 19 - UpdateCheck property

Suggested Videos
Part 16 - Difference between optimistic and pessimistic concurrency control
Part 17 - Concurrency in Linq to SQL
Part 18 - Handling ChangeConflictException

This is continuation to Part 18. Please watch Part 18, before proceeding. UpdateCheck property of ColumnAttribute class is used to determine how LINQ to SQL detects concurrency conflicts.

To set this property 
1. Open Sample.dbml file
2. Right click on AccountBalance property and select "Properties"
3. In the properties window set a value for the UpdateCheck property
update check property linq

This property can be set to one of the 3 values of the UpdateCheck enum. This enum is present in System.Data.Linq.Mapping namespace. The following are the different values of UpdateCheck enum and what they mean.

Always Always use this column for conflict detection
Never Never use this column for conflict detection
WhenChanged Use this column only when the member has been changed by the application

The default is Always. This is means by default all the columns will be used to detect concurrency conflicts.

Example : 
1. Open Sample.dbml file and right click on "AccountBalance" property and select "Properties" option from the context menu. 

2. In the properties window set, UpdateCheck = Never. At this point open, Sample.Designer.cs file and notice that the AccountBalance property has UpdateCheck property applied.

    (Storage="_AccountBalance", DbType="Int", UpdateCheck=UpdateCheck.Never)]
public System.Nullable<int> AccountBalance
          return this._AccountBalance;
          if ((this._AccountBalance != value))
              this._AccountBalance = value;

3. Open SQL Profiler and run a new trace

4. Run the application and click "Deposit $500" button

5. Inspect the generated UPDATE SQL command.

exec sp_executesql N'UPDATE [dbo].[Accounts]
SET [AccountBalance] = @p2
WHERE ([AccountNumber] = @p0) AND ([AccountName] = @p1)',
N'@p0 int,@p1 nvarchar(4000),@p2 int',@p0=1,@p1=N'John Mary',@p2=2200

Notice, that AccountBalance is removed from the WHERE clause, which means this column is not used for detecting concurrency conflicts.

LINQ to SQL Tutorial

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.