Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update row with check by linq2db in one atomic operation

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
like image 313
FireShock Avatar asked Sep 12 '14 09:09

FireShock


1 Answers

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();
like image 97
Eugene Agafonov Avatar answered Sep 28 '22 01:09

Eugene Agafonov