Update query in LINQ contains all columns in WHERE clause instead of just the primary key column

前端 未结 1 1686
别那么骄傲
别那么骄傲 2021-01-21 14:45

I am updating a single column in a table using Linq, take fictitious table below.

MyTable (PKID, ColumnToUpdate, SomeRandomColumn)

var row = (from x in Dat         


        
相关标签:
1条回答
  • 2021-01-21 15:33

    I never used LINQ-to-SQL for production projects and I never were aware of it applying optimistic concurrency1 by default.

    This is the default behavior:

    1. If a table doesn't have a Timestamp/Rowversion column2, all columns have "Update Check" set to "Always" in the DBML (except primary key columns and computed columns, i.e. all updateable columns).
    2. If a table does have a Timestamp/Rowversion column, this column has "Time Stamp" set to "True" in the DBML and all columns have "Update Check" = "Never".

    Either "Update Check" or "Time Stamp" mark a column as concurrency token. That's why in update statements you see these additional predicates on (not so) "random" columns. Apparently, the tables in your model didn't have Timestamp/Rowversion columns, hence an update checks the values of all updateable columns in the table.


    1 Optimistic concurrency: no exclusive locks are set when updating records, but existing values of all or selected columns are checked while updating. If one of those column value was changed by another user between getting the data and saving them, an update exception occurs.

    2 A column of data type Timestamp or Rowversion is automatically incremented when a record is updated and therefore detects all concurrent changes to this record.

    0 讨论(0)
提交回复
热议问题