使用 linqtosql 更新记录时间戳
我有一些包含lastupdatedate字段的表,其想法是每当行中的信息发生更改时,lastupdatedate将重置为当前日期/时间使用客户端的当前日期时间设置lastupdatedate不是一个好主意。首先,他们可能位于不同的时区。虽然我可以通过存储UCT来解决这个问题,但更严重的问题是不同用户的时钟不会全部同步。更有意义的是仅使用 GETDATE() 作为 SQL Update 命令中的 LastUpdate 参数。这样您就可以保证所有的lastupdatedate值都将相对于同一个时钟,即SQL服务器上的时钟。
在 ADO.NET 中,这很容易,因为您直接提交要执行的 SQL 语句,但在 LinqToSQL 中,您通常会执行 SubmitChanges。
除了创建存储过程之外,还有什么简单的方法可以使用 linqtosql 执行此操作吗?
I have a some tables that include a lastupdatedate fiel, the idea being that anytime the information in a row is altered, the lastupdatedate will be reset to the current date/time Setting lastupdatedate using the client side's current datetime is not a good idea. For starters, they may be in different time zones. Although I could solve this problem by storing UCT however a more serious issue is that the clocks of different users will not all be synchronized. What makes more sense is to just use GETDATE() for the lastupdate parameter in the SQL Update command. This way you are guaranteed that all lastupdatedate values will be relative to the same clock, the one on the SQL server.
In ADO.NET this was easy because you directly submitted a SQL statement to be executed but in LinqToSQL you would typically do SubmitChanges.
Is there any easy way to do this with linqtosql outside of creating a stored procedure ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以考虑的另一个选择是在数据库服务器上创建一个标量函数,该函数公开该服务器的当前时间。您将该标量函数映射到 LINQ to SQL 函数,并调用该方法来获取服务器在 SubmitChanges 之前设置对象的最后更新日期的时间。
Another option you could consider would be to create a Scalar function on the database server which exposes that server's current time. You map that scalar function to a LINQ to SQL function and call that method to get your server's time to set the lastupdatedate on your object prior to SubmitChanges.
我还存储最后修改日期并从网络服务器获取数据。到目前为止效果很好。如果您的代码仅在一台 Web 服务器上运行,则 LinqToSQL 将在该服务器上执行,因此您将拥有一致的时间源。那行得通吗?
I also store last modified date and will grab the data from the web server. It has worked fine so far. If your code is running on only one web server, then the LinqToSQL is executed on that server, therefore you will a have a consistent time source. Will that work?