NHibernate and SQL 2008 Time Data Type

How to map NHibernate type to SQL Server TIME type? I use NH 3.2 and on the map.

public class Schedule { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual TimeSpan Time { get; set; } } 

Here is my display class

 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)); } } 

Now, when I create a database from this column, "Time" is of type BIGINT SQL instead of TIME. I have read this and this article , but it is not clear to me how to apply any of these solutions.

============ EDIT =====================

I created and introduced a new schedule:

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

When this data was inserted (around 7:27 p.m.), the Time column contained a value of "700200000000", which could be ticks, but most importantly, when I requested DB for these values

 var retrievedSchedules = session.QueryOver<Schedule>().List(); 

The Time property was correctly set to 19: 27. Initially, I wanted the Time property to represent the time at which the schedule should run. Despite the fact that the SQL Server data type for this column was defined as BIGINT, and the value is represented as (most likely) ticks, after extraction it was correctly translated at the time that I wanted. This is no longer a question, but I'm going to leave it, hopefully, to someone else.

+6
source share
1 answer

This, in your comparison, should solve your problem:

 Property(x => x.Time, x => x => { x.NotNullable(true); x.Type<TimeAsTimeSpanType>(); }); 

By default, NH will use x.Type<TimeSpanType>() as the NH type for clr Time type ... which maps to SQL Server 2008 as bigint.

Have a look here: http://jameskovacs.com/2011/01/26/datetime-support-in-nhibernate/ . It covers various mappings Clr → NH → Db for dates and times.

+3
source

Source: https://habr.com/ru/post/898716/


All Articles