Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 692
chum of chance Avatar asked Nov 15 '22 05:11

chum of chance


1 Answers

Your DateTime properties are probably set to DateTime.MinValue (1/1/0001) instead of null or a value in the valid range for a DateTime column.

like image 191
Jamie Ide Avatar answered Jan 13 '23 13:01

Jamie Ide