繁体   English   中英

在一个原子操作中通过linq2db检查更新行

[英]Update row with check by linq2db in one atomic operation

MS SQL数据库表具有时间戳字段,其值在更新行之后更改。 在更新之前,我想检查该行是否已从其他服务更改。

我可以通过将内存中对象的时间戳值与数据库表中的值进行比较来实现。

我可以通过linq2db在一个原子操作中完成吗?

无需检查即可运行:

db.Update(product);

这三个查询不起作用:

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);

我想像这样执行sql-script:

update Products
set ...-- all fields
where Id = @id and Timestamp = @timestamp

我想在这里留下答案,因为linq2db是一个很棒的工具,我希望它更受欢迎。

在您的情况下,您将需要单独设置实体的所有字段,如下所示:

db.Products
  .Where(p => p.timestamp == product.timestamp)
  .Set(p => p.Name, product.Name)
  .Set(p => p.UnitPrice, product.UnitPrice)
  // ...
  .Update();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM