简体   繁体   English

如何在使用 Entity Framework Core 的 LINQ 更新语句中使用 CURRENT_TIMESTAMP?

[英]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.

相关问题 为 Entity Framework Core 更新两个列表,按时间戳 linq 更新 - Update two lists update by timestamp linq for Entity Framework Core 如何使用 Entity Framework Core 的 Linq 更新表中的多个记录 - How to update multiple records in a table Using the Linq for Entity Framework Core 如何使用LINQ和实体框架6进行表联接? - How do I do a table join using LINQ and entity framework 6? 如何使用具有多对多关系的 Entity Framework Core 5 添加和更新项目? - How do I add and update items using Entity Framework Core 5 that have many to many relationships? 如何使用 Entity Framework Core 添加/更新 C# 中的孩子的孩子? - How do I add/update the child of a child in C# using Entity Framework Core? 如何使用LINQ和Entity Framework对总计进行分组? - How can I do a group by with totals using LINQ and Entity Framework? 如何在 Azure Functions 中使用 Entity Framework Core - How do I use Entity Framework Core in Azure Functions 如何使用实体框架更新当前实体 - How to update current entities using entity framework 如何在linq实体框架c#中使用和if语句 - How to use and if statement in linq Entity framework c# 如何在Entity Framework Core 2.0上使用lambda语法在LINQ中实现LEFT OUTER JOIN? - How can I implement a LEFT OUTER JOIN in LINQ using lambda syntax on Entity Framework Core 2.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM