[英]How do I use CURRENT_TIMESTAMP in a LINQ update statement using Entity Framework Core?
I have the following T-SQL that I want to write in c# code using LINQ and entity framework core...我有以下 T-SQL,我想使用 LINQ 和实体框架核心在 c# 代码中编写...
UPDATE [MyTable1]
SET [LastContacted] = CURRENT_TIMESTAMP
FROM [MyTable1] t1
JOIN [MyTable2] t2 ON t2.[ID] = t1.[Table1ID]
WHERE t2.[MyColumn1] = 'MySearchName'
The bit I'm having a problem with is setting the CURRENT_TIMESTAMP.我遇到的问题是设置 CURRENT_TIMESTAMP。 It MUST use the time from the database and not the time from the service running the code so I can't use DateTime.Now as that would be the time from the service not the database time as there is no gaurantee the clocks are in sync.
它必须使用来自数据库的时间而不是来自运行代码的服务的时间,所以我不能使用 DateTime.Now 因为那将是来自服务的时间而不是数据库时间,因为没有保证时钟同步.
At the moment I'm using ExecuteSqlInterpolated to execute this command as hard coded SQL but really I want to use EF to update via the entity models and SaveChanges() etc, but I don't know if there is anyway to get EF/LINQ to set the value to something that can only be got when the query runs on the database.目前我正在使用 ExecuteSqlInterpolated 作为硬编码 SQL 执行此命令,但我真的想使用 EF 通过实体模型和 SaveChanges() 等进行更新,但我不知道是否有任何方法可以获得 EF/LINQ将该值设置为只有在数据库上运行查询时才能获得的值。
Does anyone know if this is possible and how to do it?有谁知道这是否可能以及如何做到这一点?
这将是我考虑使用数据库触发器而不在 C# 中做任何事情的时候之一
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.