简体   繁体   中英

Entity Framework Seed Method DateTime Error When DateTime Column Is Nullable

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?

I believe this answers your question. datetime2 does not allow DateTime.Min but DateTime does. The DateTime.min value is 00:00:00.0000000, January 1, 0001.

Link with details.

Conversion of a datetime2 data type to a datetime data type results out-of-range value

The above, as posted actually works. However, we will get this error if:

  1. We have a non-nullable DateTime property other than CreatedDate that we might have missed, and did not specify in the Seed method.
  2. VERY EASY TO FORGET: If you created custom AspNet.Identity entities with custom properties, you may have put a non-nullable DateTime property in them, as was my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM