简体   繁体   English

ServiceStack + OrmLite日期格式问题

[英]ServiceStack + OrmLite Date format issue

I'm using ServiceStack + OrmLite and I'm having some trouble with OrmLite saving DateTimeOffset objects. 我正在使用ServiceStack + OrmLite,并且在OrmLite保存DateTimeOffset对象时遇到了一些麻烦。

I need to save it into a SQL Server DB with a neutral culture format (for example ISO8601) so when the user changes his culture it won't have any impact on how it's written to the DB because now it's generating Conversion failed when converting date and/or time from character string. 我需要将其保存到具有中性区域性格式(例如ISO8601)的SQL Server数据库中,因此,当用户更改其区域性时,它不会对其写入数据库的方式产生任何影响,因为现在Conversion failed when converting date and/or time from character string.生成Conversion failed when converting date and/or time from character string. errors due to culture conversion issues. 由于文化转换问题而导致的错误。

Is there any OrmLite configuration option or something similar to avoid this problem? 是否有任何OrmLite配置选项或类似方法可避免此问题?

Date/time types in SQL Server, including datetime and datetimeoffset are not stored with any culture. SQL Server中的日期/时间类型(包括datetimedatetimeoffset不会与任何区域性一起存储。 They're not stored as strings at all. 它们根本不存储为字符串。 They are internally stored with a binary representation. 它们在内部以二进制表示形式存储。

If your class in C# uses a property with a DateTimeOffset type, and your table in SQL Server uses a column with a datetimeoffset type, then strings (and culture) should never enter the picture. 如果C#中的类使用具有DateTimeOffset类型的属性,并且SQL Server中的表使用具有datetimeoffset类型的列,则字符串(和区域性)不应输入图片。

You might get strings if you manually crafted a SQL string and inlined your values instead of using parameters. 如果您手动制作SQL字符串并内联值而不是使用参数,则可能会得到字符串。 But I doubt an ORM would make this mistake. 但是我怀疑ORM会犯此错误。

Or are you trying to stuff the string representation of a DateTimeOffset into a varchar field? 还是您正在尝试将DateTimeOffset的字符串表示形式填充到varchar字段中?

Post some code so we can see what you're doing. 发布一些代码,以便我们可以看到您在做什么。

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

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