Mike Nichols - Son of Nun Technology

Persisting DateTime values and Unit Testing

This is a note for me to recall when doing testing between my app and the database.

Typically, I initialize DateTime variables to a specification in my application using a Specification like this:

 

    public class DateTimeSpecification

    {

        public static DateTime UNSPECIFIED_MIN = new DateTime(1800,1,1).Date;

        public static DateTime UNSPECIFIED_MAX = new DateTime(2999,12,31).Date;

    }

This works great but of course I forget sometimes. There was a good discussion on Oren's blog about this issue a while back but I didn't ahve time to keep up with it. My solution to get around the dreaded SqlDateTimeOverflowException is to use this Specification in my apps.

I am thinking about writing a patch for NHibernate that would have a floor and ceiling for DateTime values according to the DB provider...eventually.

Anyways, I was using SqlCE to do some testing and the message is a bit cryptic:

System.Data.SqlServerCe.SqlCeException: An overflow occurred while converting to datetime.

It's the same issue, just a less meaningful message...