6

MS SQL database table has timestamp field which value changing after update row. Before update I want to check if the row is changed from another service.

I can do it by comparing timestamp values from object in memory with value from database table.

Can I do it by linq2db in one atomic operation?

It works without checking:

db.Update(product);

These three queries do not work:

db.Products.Where(p => p.timestamp == product.timestamp).Update(p => p, product);
db.Products.Update(p => p.timestamp == product.timestamp, p => product );
db.Where<DB, Product, DB>(p => p.timestamp == product.timestamp).Update(product);

I want to execute sql-script like this:

update Products
set ...-- all fields
where Id = @id and Timestamp = @timestamp
reptile
  • 175
  • 1
  • 9
FireShock
  • 1,082
  • 1
  • 15
  • 25

1 Answers1

7

I'd like to leave an answer here, since linq2db is an awesome tool, which I would like to be more popular.

In your case you will need to set all fields of your entity separately, like this:

db.Products
  .Where(p => p.timestamp == product.timestamp)
  .Set(p => p.Name, product.Name)
  .Set(p => p.UnitPrice, product.UnitPrice)
  // ...
  .Update();
Eugene Agafonov
  • 421
  • 3
  • 6
  • Unfortunately, that seems to be awful due to the fact that we need to write as a long construction as T-SQL code, firstly. In addition, remember to update it simultaneously with changing the DB schema. – FireShock May 15 '16 at 16:26
  • 1
    You can easily write a helper method to deal with it. The other suggestion is pull request to linq2db repo :) – Eugene Agafonov May 18 '16 at 15:33