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

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

问题描述

我不想让我目前的ModifeidDateTime已经是服务器时间的机器时间了。每次我想在SQL Server 2008上更新或添加一个人到我的datebase时,我想填写ModifiedDateTime。在处理数据集时,我可以像data adapter命令一样更改更新查询,并将我的ModifiedDateTime归档定义为= GetDate()。我创建了存储函数来重写一个GetDate()方法的值,但是我有一个导入过程的问题,它返回值为int,string ...或者没有任何值,已经只有实体值作为Person,例如在我的情况。这是为什么?无论如何,如果您能为我提供任何可以从服务器检索DateTime值的解决方案,那将是非常有帮助的。

I don't want for my current ModifeidDateTime to be machine time already server time. 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 retutn 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 provide me any solution that works to retrieve DateTime value from server.

推荐答案

是有一个原因,你根本不能把它推到你的数据库?如果您的实体查询中包含DateTime.Now,那么它会将其推送到数据库(

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.

实体列表

 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]

可能是一个更好的方法吗?

Might be a better way to do it ?

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

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