使用 NHibernate(和 Fluent NH)以优于秒的精度(并且不截断毫秒)映射日期时间类型版本列
我有一个表,其中包含 datetime
类型列作为版本。它是一个遗留数据库,因此我不可能将其更改为 datetime2 或使用不同的版本控制机制。 NHibernate 类将其映射到 DateTime
c# 类型属性。
我已经看到了几个关于此问题的问题以及论坛帖子和回复,但无论我尝试了什么,NHibernate 都会不断截断 DateTime 值的毫秒数。
以下是我目前使用 Fluent NHibernate 所做的事情:
Version(x => x.ModifiedOn).Column("ModifiedOn")
.CustomType("Timestamp").Not.Nullable();
在我正在映射的类中,我有:
public virtual System.DateTime ModifiedOn { get; set; }
数据库是 MS SQL 2008,Fluent NH 的配置如下:
Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString......)
我需要什么:如何配置 Fluent NH 的工作示例使 NH 发送以毫秒为单位的日期时间值(从我在代码中看到的情况来看,它的精度应该是 10 毫秒)。谢谢!
I have a table with a datetime
-type column as the version. It's a legacy DB so I can't possibly change it to datetime2
or use a different versioning mechanism. The NHibernate class is mapping this to a DateTime
c# typed property.
I've seen several questions and also forum posts and replies regarding this issue, but regardless of what I've tried, NHibernate keeps truncating the milliseconds off the DateTime value.
Here's what I'm currently doing with Fluent NHibernate:
Version(x => x.ModifiedOn).Column("ModifiedOn")
.CustomType("Timestamp").Not.Nullable();
And in my class that's being mapped, I have:
public virtual System.DateTime ModifiedOn { get; set; }
The database is MS SQL 2008, and Fluent NH is configured as such:
Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString......)
What I need: a working example of how to configure Fluent NH to make NH send datetime values with milliseconds (from what I've seen in the code it should be 10 ms accuracy). Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的行为(截断毫秒)是 NHibernate 对于
DateTime
值的默认行为,因此听起来 Fluent NHibernate 忽略了.CustomType("Timestamp")
。您能否导出映射文件并在此处发布 *.hbm.xml 文件之一?这将帮助我们确定问题是出现在 NHibernate、Fluent NHibernate 还是其他地方。请参阅 Fluent NHibernate 文档,了解导出映射文件的说明。如果 Fluent NHibernate 工作正常,那么您应该会看到类似的内容,
我认为您会看到
type="System.DateTime"
。最可能的原因是您的 Fluent NHibernate 代码根本没有被执行。发布有关如何使用 Fluent NHibernate 创建会话工厂的详细信息将有助于我们确定这是否是原因。The behavior you're describing (truncating milliseconds) is NHibernate's default behavior for
DateTime
values, so it sounds as if Fluent NHibernate is ignoring the.CustomType("Timestamp")
. Could you please export the mapping files and post one of the *.hbm.xml files here? That will help us to determine whether the problem is in NHibernate, Fluent NHibernate, or possibly somewhere else. See the Fluent NHibernate documentation for instructions on exporting the mapping files.If Fluent NHibernate is working correctly, then you should see something similar to
I think instead you'll see
type="System.DateTime"
. The most likely cause is that your Fluent NHibernate code is simply not being executed. Posting details about how you use Fluent NHibernate to create your session factory would help us determine whether that's the cause or not.