Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

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.

[global::System.Data.Linq.Mapping.ColumnAttribute
    (Storage="_AccountBalance", DbType="Int", UpdateCheck=UpdateCheck.Never)]
public System.Nullable<int> AccountBalance
{
     get
     {
          return this._AccountBalance;
     }
     set
     {
          if ((this._AccountBalance != value))
          {
              this.OnAccountBalanceChanging(value);
              this.SendPropertyChanging();
              this._AccountBalance = value;
              this.SendPropertyChanged("AccountBalance");
              this.OnAccountBalanceChanged();
          }
     }
}

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

It would be great if you can help share these free resources