0

I have a column as follows:

[Column(TypeName="smalldatetime")]
public DateTime LastUpdatedOn { get; set; }

In the Seed method I have:

context.Database.ExecuteSqlCommand("Alter Table Members Add Default getdate() for LastUpdatedOn");

and also inserted some sample rows.

The database is created fine, including the default values, but when the sample rows are inserted I get:

"The conversion of a datetime2 data type to a smalldatetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."

Why would the current time be out of range for smalldatetime? I want to stick to smalldatetime for the database. How do I do it?

PS: When the database column is the default datetime type, I also get the same error.

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • Duplicate of http://stackoverflow.com/questions/6050906/how-to-fix-the-datetime2-out-of-range-conversion-error-using-dbcontext-and-setin. LastUpdatedOn will be `DateTime.MinValue` by default. Either use a `DateTime?` where empty values will be saved as `NULL`, or set a within-range value in the constructor. – ChrisV Feb 09 '15 at 07:01
  • I see. So EF does not leave the column as null but populates it with DateTime.MinValue? – Old Geezer Feb 09 '15 at 15:30
  • The default value in C# for DateTime is `DateTime.MinValue`. EF just saves that value. – ChrisV Feb 09 '15 at 22:10

0 Answers0