Custom SQL function for NHibernate dialect

Posted by Kristoffer Ahl on Stack Overflow See other posts from Stack Overflow or by Kristoffer Ahl
Published on 2009-12-04T09:37:45Z Indexed on 2010/03/19 18:01 UTC
Read the original article Hit count: 259

Filed under:
|
|

I want to be able to call a custom function called "recent_date" as part of my HQL. Like this: [Date] >= recent_date()

I created a new dialect, inheriting from MsSql2000Dialect and specified the dialect for my configuration.

public class NordicMsSql2000Dialect : MsSql2000Dialect
{
    public NordicMsSql2000Dialect()
    {
    	RegisterFunction(
    		"recent_date",
    		new SQLFunctionTemplate(
    			NHibernateUtil.Date,
    			"dateadd(day, -15, getdate())"
    			)
    		);
    }
}

var configuration = Fluently.Configure()
.Database(
    MsSqlConfiguration.MsSql2000
    .ConnectionString(c => ....	)
    .Cache(c => c.UseQueryCache().ProviderClass<HashtableCacheProvider>())
    .Dialect<NordicMsSql2000Dialect>()
)
.Mappings(m => ....)
.BuildConfiguration();

When calling recent_date() I get the following error: System.Data.SqlClient.SqlException: 'recent_date' is not a recognized function name

I'm using it in a where statement for a HasMany-mapping like below.

HasMany(x => x.RecentValues)
    .Access.CamelCaseField(Prefix.Underscore)
    .Cascade.SaveUpdate()
    .Where("Date >= recent_date()");

What am I missing here?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate