NHibernate和SQL 2008时间数据类型

use*_*375 6 nhibernate

如何将NHibernate类型映射到SQL Server的TIME类型?我正在使用NH 3.2和逐个代码.

public class Schedule
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual TimeSpan Time { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我的映射类

public class ScheduleMapping : ClassMapping<Schedule>
{
    public ScheduleMapping()
    {
        Id(x => x.Id, x => x.Generator(Generators.Native));
        Property(x => x.Name, x => x.NotNullable(true));
        Property(x => x.Time, x => x.NotNullable(true));
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我从此列创建数据库时,"Time"具有BIGINT SQL类型而不是TIME.我已阅读文章,但它不是我清楚如何应用这些解决方案.

===========编辑====================

我创建并插入了这样的新计划:

var newSchedule = new Schedule {
                   Name = "My Schedule",
                   Time = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)
                   };
session.SaveOrUpdate(newSchedule);
Run Code Online (Sandbox Code Playgroud)

当插入此数据时(大约晚上7点27分),时间列包含值'700200000000',这可能是滴答声,但最重要的是当我查询数据库中的这些值时

var retrievedSchedules = session.QueryOver<Schedule>().List();
Run Code Online (Sandbox Code Playgroud)

Time属性已正确设置为19:27.最初我希望Time属性表示Schedule应该运行的时间.尽管此列的SQL Server数据类型被定义为BIGINT,并且该值被表示为(最可能)滴答,但在检索之后它被正确转换为时间,这是我想要的.这不再是一个问题,但我会留下这个,希望是其他人的利益.

小智 3

这在您的映射中应该可以解决您的问题:

Property(x => x.Time, x => x => { x.NotNullable(true); x.Type<TimeAsTimeSpanType>(); });
Run Code Online (Sandbox Code Playgroud)

默认情况下,NH 将用作x.Type<TimeSpanType>()clrTime类型的 NH 类型...它作为 bigint 映射到 SQL Server 2008。

看看这里: http: //jameskovacs.com/2011/01/26/datetime-support-in-nhibernate/。它涵盖了日期和时间的不同 Clr --> NH --> Db 类型映射。