如何使用实体框架向数据库服务器询问当前日期时间? [英] How to ask the database server for current datetime using entity framework?

查看:16
本文介绍了如何使用实体框架向数据库服务器询问当前日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ModifiedDateTime 属性的实体,我想用数据库中的当前日期时间而不是执行应用程序的应用程序"服务器来更新它.

I have an entity with a ModifiedDateTime property which I want to be updated with the current datetime from the database instead of the "application" server executing the application.

每次我想在 SQL Server 2008 上更新或添加一个人到我的数据库时,我都想填写 ModifiedDateTime 文件.当我使用数据集并将我的 ModifiedDateTime 文件定义为 GetDate() 时,我不能像使用数据适配器命令一样更改更新查询.我创建了存储函数来返回 GetDate() 方法的值,但是我在导入过程时遇到问题,该过程返回值作为 int、string 或根本没有值,在我的例子中已经只是实体值作为 Person.这是为什么?

Every time I want to update or add a person to my datebase on SQL Server 2008 I want to fill ModifiedDateTime filed. It's not like I can change update query as with data adapter command when I work with dataset and to define for my ModifiedDateTime filed to be GetDate(). I created stored function to return me a value of GetDate() method, but I have a problem to import procedure which returns values as int, string or no value at all, already just entity values as Person for example in my case. Why is that?

无论如何,如果您能帮我从数据库服务器中检索当前的日期时间,那将是非常有帮助的.

Anyway, it would be of great help if you can help me to retrieve the current DateTime from the database server.

推荐答案

你不能把它推送到你的数据库有什么原因吗?如果您在实体查询中包含 DateTime.Now,它会将其下推(getdate)到数据库.

Is there a reason you just can't push it down to your database? If you include DateTime.Now in your entity query, it will push it down (getdate) to the database.

到实体的示例 linq

Example linq to entities

 var dQuery = dbContext.CreateQuery<DateTime>("CurrentDateTime() ");
 DateTime dbDate = dQuery.AsEnumerable().First();

SQL 生成..

SELECT GetDate() AS [C1] FROM  ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]

可能是更好的方法吗?

这篇关于如何使用实体框架向数据库服务器询问当前日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆