ContentPart with DateTime gives "SqlDateTime overflow"

Topics: Writing modules
Aug 20, 2011 at 2:59 PM

I've tried to add a DateTime to my contentPart, but when saving i get a "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.", this looks like a 'new date' (01.01.0001). When im debugging I can see in the MapDriver that the MapPart is updated from the EditView. To simplyfy and reproduce my content part i modified the Maps example. I've only changed the Map.cs and Migrations.cs to use DateTime, see below. ( I know the view will not display a map, if the data is saved. )

What is necessary to save a datetime?


public class MapRecord : ContentPartRecord {
        public virtual DateTime Latitude { get; set; }
        public virtual DateTime Longitude { get; set; }

    public class MapPart : ContentPart<MapRecord> {
        public DateTime Latitude
            get { return Record.Latitude; }
            set { Record.Latitude = value; }

        public DateTime Longitude
            get { return Record.Longitude; }
            set { Record.Longitude = value; }
public class Migrations : DataMigrationImpl {

        public int Create() {
			// Creating table MapRecord
			SchemaBuilder.CreateTable("MapRecord", table => table
				.Column("Latitude", DbType.DateTime)
                .Column("Longitude", DbType.DateTime)

                typeof(MapPart).Name, cfg => cfg.Attachable());

            return 1;
Aug 20, 2011 at 4:18 PM

As DateTime is not nullable, and you don't provide a default value, I assume it will try to set a DateTime.MinValue which is incompatible with SQL Server. Thus you should assign default values in the Record's constructor, or define a default in the migration class.

Aug 20, 2011 at 6:37 PM

Okay, I'll try that. But I'm not trying to save a null value. The value that I tried to save was valid and I could see in the debugger that was assigned to the model in the driver. But I still get the error.

Sep 20, 2011 at 11:49 AM
Edited Sep 20, 2011 at 12:11 PM

I'm also getting this error, when displaying the EditView and this happens before trying to save it.

I don't wan't to have a default date that gets displayed when editing the part

My solution is this:

1.) I set the default date of 1/1/1900 in my Record's constructor

2.) I have the following helper function for the edit view

@helper EmptyDefaultDate(DateTime d)
    if (d.CompareTo(new DateTime(1900,1,1)) == 0)
Or is there a better way?

There is still one problem though. 
The date field is not marked with [Required] and despite setting the default value in the Record's constructor 
I get the "The DateBirth field is required." when I try to save my part with an empty value of the DateBirth field.
Sep 20, 2011 at 4:45 PM
Edited Sep 20, 2011 at 4:45 PM

Never mind, found the answer in Discussions just use DataTime? and Nullable() in migrations