Search code examples
nhibernatefluent-nhibernatemappingfluent

Nhibernate Fluent datetime mapping creates null column in DB


I really struggle to find an answer anywhere.

I got this class declaration:

public class Order
{
   public virtual long Id { get; set; }

   public virtual DateTime OrderDate { get; set; }
}

I have created a mapping class where I map the property OrderDate using the following:

Map(x => x.OrderDate)
    .CustomSqlType("datetime2")
    .Not.Nullable();

When it is exported to the database it creates NULL column, ignoring Not.Nullable instruction.

Any help will be really appreciated.


Solution

  • I would say, that the mapping should be like this:

    Map(x => x.OrderDate)
        .CustomType("datetime2") // not CustomSqlType
        .Not.Nullable();
    

    Expecting that you are using SQL Server 2008+, check this link for more info about relationship DB., CLR and NHibernate types NHibernate and Ms Sql Server 2008: Date, Time, DateTime2 and DateTimeOffset