Using custom dialect functions

In this recipe, we will show how you can add mappings for database functions, which are not available by default.

How to do it…

  1. Create a new class library project named CustomDialectExample.
  2. Install the NHibernate and log4net packages using the NuGet Package Manager Console by executing the following command:
    Install-Package NHibernate
    Install-Package log4net
    
  3. Create a new class named CustomMsSql2012Dialect using the following code:
    public class CustomMsSq2012Dialect: MsSql2012Dialect { public CustomMsSq2012Dialect() { RegisterFunction("AddDays", new SQLFunctionTemplate( NHibernateUtil.DateTime, "dateadd(day,?2,?1)")); RegisterFunction("AddHours", new SQLFunctionTemplate( NHibernateUtil.DateTime, "dateadd(hour,?2,?1)")); ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.