I'm using FNH to generate the mappings of about 30 classes of a library used in an ASP.NET MVC3 application. I'm working against MSSQL Express 10. I created some code allowing me to populate the database with some data for development purposes. However when I try to save an Account instance, I get this error message:
System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
This happens because of the Account.CreationDate attribute, which is a DateTime. I've looked around and the DateTime in MSSQL and in .NET are not actually the same. I tried, via the FNH mappings force the column to be of "datetime2" but that didn't seem to help.
This is my Account class (simplified):
public class Account
{
public virtual int Id { get; set; }
public virtual string Comment { get; set;}
public virtual DateTime CreationDate { get; set;}
public virtual string Email { get; set;}
public virtual string Password {get ; set;}
public virtual string Locale {get; set;}
public Account(string password, string email)
{
this.Email = email;
SetNewPassord(password);
this.CreationDate = DateTime.Now;
this.Locale = "en-US";
}
}
And the FNH mapping:
public class AccountMap : ClassMap<Account>
{
public AccountMap()
{
Id(x => x.Id);
Map(x => x.Comment);
Map(x => x.CreationDate);
Map(x => x.Email);
Map(x => x.Password);
Map(x => x.Locale);
}
}
And the code where I call this:
var compteAdmin = new Account("test", "[email protected]");
compteAdmin.Locale = "fr-FR";
var toto = compteAdmin.CreationDate;
try
{
session.Save(compteAdmin);
}
catch (Exception ex)
{
throw new ConfigurationException("Account.CreationDate: " + compteAdmin.CreationDate.ToString() + " ; Exception: " + ex);
}
Note that for the purpose of debugging I'm throwing some exception. The output of that is kind of surreal!
Account.Creation date: 2/6/2013 5:32:29 PM ; Exception: System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.