When reading a value from a "datetime2" column via Session.CreateSQLQuery() the milliseconds are lost.Same query using "Session.Query<T>()" works.
Error cause:In class "CustomLoader" method "GetHibernateType()":
"GetFieldType()" returns for a "datetime2" column "Datetime" which resolves to the "DateTimeType" from NHibernate which cuts the milliseconds.
If we use "GetDataTypeName()" for getting column type we get "datetime2" which then resolves correctly to "DateTime2Type".
So I think using "GetFieldType()" should only be used as a fallback if resolving via "GetDataTypeName()" didn't return a result.
Moved here.
In NHibernate 5.0 (https://nhibernate.jira.com/browse/NH-3919#icft=NH-3919), the default DateTime type does no more cut fractional seconds. Moreover, with SQL Server 2008 dialect and later, the default DbType used for DateTime is DateTime2.
DateTime
DbType
DateTime2
Moving to improvement.
Here is a simple workaround:
see testcase in https://github.com/nhibernate/nhibernate-core/pull/598/commits/3fe7e4358bbe112f47fba684172b72af4964506d
When reading a value from a "datetime2" column via Session.CreateSQLQuery() the milliseconds are lost.
Same query using "Session.Query<T>()" works.
Error cause:
In class "CustomLoader" method "GetHibernateType()":
"GetFieldType()" returns for a "datetime2" column "Datetime" which resolves to the "DateTimeType" from NHibernate which cuts the milliseconds.
If we use "GetDataTypeName()" for getting column type we get "datetime2" which then resolves correctly to "DateTime2Type".
So I think using "GetFieldType()" should only be used as a fallback if resolving via "GetDataTypeName()" didn't return a result.