4

I'm mapping a very simple Users table, and i have a column named 'LastLoginDate' which is defined as nullable in sql server.

My mapping looks like this :

public Users {
    Id(x => x.UserId);
    Map(x => x.UserName);
    ...
    ...
    Map(x => x.LastLoginDate).Nullable();
}

But everytime I try to save this entity programatically, i always get the SqlDateTime overflow exception. If i try to enter a manual sql statement with 'null' in this column it works. If i comment out just this property, it will work as well.

What can be the problem ???

Thanks in advance!

gillyb
  • 8,760
  • 8
  • 53
  • 80

1 Answers1

12

Your entity should look like this:

public class User
{
   public virtual DateTime? LastLoginDate {get;set;}
   // etc
}

Then, your map should work properly.

edit: The ? after DateTime specifies that it is Nullable, and is a short form for Nullable<DateTime>. If this isn't the cause of your error, you may want to check that Fluently.Configure specifies the correct version of SqlServer.

Joseph Yaduvanshi
  • 20,241
  • 5
  • 61
  • 69
  • WOW, i feel so stupid now!... Sometimes it's so hard to see that you forgot something so trivial and small :) Thanks! – gillyb Apr 24 '10 at 21:46
  • No problem, I do it all the time. Sometimes you just need a second pair of eyes. – Joseph Yaduvanshi Apr 24 '10 at 21:52
  • I'm having exactly the same problem, but making DateTime nullable simply isn't working for me. Not sure what could be wrong. – Jonathan Jun 19 '11 at 09:38
  • I'd double check that the column is marked nullable in the database. If it is, clean your solution and rebuild. Also, if you're using SQL Server 2008 it may be a `DateTime.MinValue` see: http://stackoverflow.com/questions/5425610/fluent-nhibernate-problems-with-sql-server-2008-date-column-values/5505222#550522z In that case, you'll need to override: http://stackoverflow.com/questions/5506524/fluent-nhibernate-override-type-for-one-specific-property-on-one-specific-class/5506569#5506569 – Joseph Yaduvanshi Jun 19 '11 at 14:39
  • If still having problem after clean and rebuild, another thing to do is clear the second level cache. In my case the DateTime.MinValue was held in the cache and kept on getting re-applied as the data was read from the cache. – Frank Bell Jan 11 '16 at 22:27