Search code examples
nhibernatefluent-nhibernate

NHibernate 2.1.2 throwing DateTime null overflow exception


I'm having a similar problem to these questions:

NHibernate 2.* mapping files: how to define nullable DateTime type (DateTime?)?

NHibernate won't persist DateTime SqlDateTime overflow

I'm using NHibernate 2.1.2 and FluentNhibernate 1.0.0.636. With NHibernate 2.x the nullable DateTime? issue should be fixed and I shouldn't have to do anything special with my mapping. Accordingly, all my DateTime properties are simply set like so:

public virtual DateTime? CreatedOn { get; set; }

In my database (SQL2008), all DateTime properties are set to allow null. I have my NHibernate config file setup to use the SQL2008 dialect:

<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>

NHibernate works fine for anything that does not include a DateTime. For reference, here's the exact error I'm receiving:

> at
> NHibernate.AdoNet.SqlClientSqlCommandSet.ExecuteNonQuery()</StackTrace><ExceptionString>System.Data.SqlTypes.SqlTypeException:
> SqlDateTime overflow. Must be between
> 1/1/1753 12:00:00 AM and 12/31/9999
> 11:59:59 PM.

If I run SQL Profiler, I can see the last command that NHibernate attempts to execute (this was a very long statement, so I've truncated it):

exec sp_executesql N'UPDATE Projects SET Job = @p0, CreatedOn = @p1, .. WHERE (Where Clause), @p0=219221, @p1=NULL

If I execute this statement as a query, SQL persists it fine, doesn't complain at all!

What's going on?


Solution

  • I don't know why, but this works here. Unlike the example in the link, mine are DateTime? so .... I do not know why it works, but it does.