简体   繁体   English

实体框架和SQL Server日期时间

[英]Entity Framework and SQL Server datetime

I'm using EF with SQL Server and when I'm fetching a DateTime field that is DateTime on the MSSQL and on EF, I would get something like this: 我正在将EF与SQL Server一起使用,当我在MSSQL和EF上获取一个DateTime字段时,我将得到如下内容:

"UpdateTime":"\/Date(1358716865533+0200)\/"

I have no idea what format is this (only that it shows some time with GMT+2 offset). 我不知道这是什么格式(只是用GMT + 2偏移显示了一段时间)。

Any suggestions? 有什么建议么?

Second, I've tried to use ToString("") on that fields inside the LINQ that fetches the records and got an error that SQL doesn't support formatting this way - so I have no easy way to format that date as I want. 其次,我尝试在LINQ的内部字段上使用ToString(“”)来获取记录,并得到一个错误,提示SQL不支持这种格式设置-所以我没有简便的方法来格式化该日期。 Is there any way to apply formatting? 有什么方法可以应用格式设置?

Thanks! 谢谢!

As said in the comment, this is a Unix format. 如评论中所述,这是Unix格式。 You'll get a similar string when you serialize a DateTime with JavaScriptSerializer (JSON). 当您使用JavaScriptSerializer (JSON)序列化DateTime时,会得到类似的字符串。

As for your second question. 至于第二个问题。 Yes it can be a nuisance that EF does not allow ToString in linq queries. 是的,EF不允许linq查询中的ToString令人讨厌。 On the other hand, EF is for data access, formatting is for UI. 另一方面,EF用于数据访问,格式化用于UI。 So you better do the formatting after you've fetched data from the database into memory display them somewhere. 因此,最好将数据从数据库中提取到内存中后再显示在某处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM