Pages

Monday, April 22, 2013

Set UpdateCheck to Never programatically for LINQ Data Contexts

By default when you generate a LINQ Data Context in Visual Studio, every column of every table has the UpdateCheck property set to Always. The implication of this is that the UPDATE sql statements issued by LINQ will include all the fields in the WHERE clause in order to match the exact copy of the record that was initially read and prevent overwriting changes made by other users/threads between the last time we read the record and the moment we try to update it. And that's fine in many scenarios but sometimes you want all the UPDATE statements to find the record to update and change it regardless of changes sent by others (a "last commit wins" type of policy) and for that you'd need LINQ's UPDATE statements to only include the table's primary key fields in the WHERE clause. That is, you need the UpdateCheck property set to Never for all the columns that are not primary keys.