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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With