Search code examples
sqlnhibernatefluent-nhibernatefluent-nhibernate-mappingsmalldatetime

FluentNHibernate mapping smalldatetime SQL data type


I have a legacy database that uses the smalldatetime SQL data type. That maps fine to the standard DateTime. However, when I use SchemaExport, it understandably generates the column with datetime format. What custom type should I be using in my mapping so that the generated column is smalldatetime?

   // Does not work as custom type not known       
   Map(x => x.BirthDate).Column("dtBirthDate").Not.Nullable().CustomType("smalldatetime");

Solution

  • You almost had it, instead of .CustomType you'll have to define .CustomSqlType

    Map(x => x.BirthDate)
        .Column("dtBirthDate")
        .Not.Nullable()
        .CustomSqlType("smalldatetime")
        .CustomType("datetime")
    

    Just tested it and it will create a database column with smalldatetime.