1

A method of preventing concurrency in LINQ-to-SQL is to add rowversion/timestamp column to the SQL table.

But I observed that once the table is imported into the visual studio as a data class the UpdateCheck for all properties/column is set to 'NEVER'.

It is noteworthy that the RowVersion and other properties whose UpdateCheck are set to NEVER are actually used to update the table. This can be seen in the transact-sql code below. Kindly explain. Thank you.

exec sp_executesql N'UPDATE [dbo].[Accounts]
SET [AccountBalance] = @p2
WHERE ([AccountNumber] = @p0) AND ([Version] = @p1)

SELECT [t1].[Version]
FROM [dbo].[Accounts] AS [t1]
WHERE ((@@ROWCOUNT) > 0) AND ([t1].[AccountNumber] = @p3)',N'@p0 int,@p1 timestamp,@p2 int,@p3 
int',@p0=1,@p1=0x00000000000007D3,@p2=1500,@p3=1

See the corresponding c# code below:

    protected void btnDeposit_Click(object sender, EventArgs e)
    {
        using (DataClasses1DataContext dbContext = new DataClasses1DataContext())
        {
            try
            {
                Account account = dbContext.Accounts.FirstOrDefault(x => x.AccountNumber == 1);
                account.AccountBalance += 500;
                dbContext.SubmitChanges();
                GetAccountsData();
            }
            catch (ChangeConflictException)
            {
                dbContext.ChangeConflicts.ResolveAll(RefreshMode.OverwriteCurrentValues);
                dbContext.SubmitChanges();
                GetAccountsData();
            }
        }
    }

UpdateCheck for all properties set to 'NEVER'

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Accounts")]
public partial class Account : INotifyPropertyChanging, INotifyPropertyChanged
{
    
    private static PropertyChangingEventArgs emptyChangingEventArgs = new 
    PropertyChangingEventArgs(String.Empty);
    
    private int _AccountNumber;
    
    private string _AccountName;
    
    private System.Nullable<int> _AccountBalance;
    
    private System.Data.Linq.Binary _Version;

    public Account()
    {
        OnCreated();
    }
    
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AccountNumber", DbType="Int NOT 
    NULL", IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)]
    public int AccountNumber . . .
    
    
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AccountName", DbType="NVarChar(50)", 
    UpdateCheck=UpdateCheck.Never)]
    public string AccountName . . .
    
    
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AccountBalance", DbType="Int", 
    UpdateCheck=UpdateCheck.Never)]
    public System.Nullable<int> AccountBalance . . .
    
    
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Version", AutoSync=AutoSync.Always, DbType="rowversion NOT NULL", CanBeNull=false, IsDbGenerated=true, IsVersion=true, UpdateCheck=UpdateCheck.Never)]
    public System.Data.Linq.Binary Version . . .
    
Akif
  • 7,098
  • 7
  • 27
  • 53

0 Answers0