Closed
Description
Context
Updating v5.1.7 to 5.3.10 together with FluentNHibernate 3.1.0
NHibernate LINQ providers don't support DateTime.TimeOfDay
to automatically convert to SQL time
Approach
RegisterFunction()
to the rescue to adjust the dialect, where in 5.1.7 the following code works for MS SQL 2012 dialect
RegisterFunction(nameof(Ext.GetTime),
new SQLFunctionTemplate(NHibernateUtil.TimeAsTimeSpan, "cast(?1 as time)"));
where Ext.GetTime
only accesses TimeOfDay
public static class Ext
{
public static TimeSpan GetTime(this DateTime dateTime)
{
return dateTime.TimeOfDay;
}
}
Usage
var start = filter.Start.GetTime();
var end = filter.End.GetTime();
return Session.Query<Entity>().Where(x => x.Start.GetTime() < end && x.End.GetTime() > start).ToList();
class Entity
{
public virtual DateTime Start { get; set; }
public virtual DateTime End { get; set; }
}
Exception in 5.3.10
While v5.3.10 throws an exception
---- System.Data.SqlClient.SqlException : Operand type clash: time is incompatible with bigint
I understand TimeSpan can have more than 24h, whereas this method is used only for less than 24h intervals.
Related: TimeAsTimeSpanType has changes from v5.1.3 related to casting types to TimeSpan
.
Questions
Trying to figure out
- Where
InternalTicks
fromTimeOfDay
tries to map to SQL typetime
in NHibernate - Documentation related to this breaking change
- Why v5.1.7 works and what is the new approach in v5.3.10 version
Open to ideas how to contribute to documentation 😃