I have the following POCO:
public class Specialty
{
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? CreatedDate { get; set; }
public string Description { get; set; }
public string Id { get; set; }
public string Name { get; set; }
}
In Configuration.cs's Seed Method, I have:
public override void Seed(MyContext context)
{
context.Specialties.AddOrUpdate(p => p.Name,
new Specialty { Id = Guid.NewGuid().ToString(),
Description = "Allergy and Immunology",
Name = "Allergy and Immunology" },
new Specialty { Id = Guid.NewGuid().ToString(),
Description = "Anaesthesiology",
Name = "Anaesthesiology" });
}
The migration "command" for this table:
CreateTable("dbo.Specialty",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
CreatedDate = c.DateTime(defaultValueSql: "GETDATE()"),
Description = c.String(),
Name = c.String(),
})
.PrimaryKey(t => t.Id);
So, in the Seed function, I purposely left out CreatedDate because I figured SQL would take care of populating this upon insertion because of my annotation (database generated, computed) and Migration setting defaultValueSql: "GETDATE()"
.
I am getting that error "The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value." when I run Update-Database. To my knowledge, not specifying CreatedDate in the Seeding method will try to save a null CreatedDate into the database, which is going to be allowed. It seems like EF did something behind my back - what?
My guess is that EF forced DateTime.Now.Min or something which is out-of-range for the SQL datetime data type... but since I've specified database generated, shouldn't it stop EF from creating an out-of-range value for insert?