简体   繁体   English

当DateTime列可为空时,实体框架种子方法DateTime错误

[英]Entity Framework Seed Method DateTime Error When DateTime Column Is Nullable

I have the following POCO: 我有以下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: 在Configuration.cs的种子方法中,我有:

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()" . 因此,在Seed函数中,我故意遗漏了CreatedDate,因为我认为SQL会在插入时填充它,因为我的注释(数据库生成,计算)和迁移设置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." 我收到该错误“将datetime2数据类型转换为日期时间数据类型导致超出范围的值。” when I run Update-Database. 当我运行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. 据我所知,未在Seeding方法中指定CreatedDate将尝试将null CreatedDate保存到数据库中,这将被允许。 It seems like EF did something behind my back - what? 看起来EF在我背后做了什么 - 什么?

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? 我的猜测是EF强制DateTime.Now.Min或SQL日期时间数据类型超出范围的东西......但是因为我已经指定了数据库生成,所以不应该阻止EF创建一个out-of - 插入的范围值?

I believe this answers your question. 我相信这回答了你的问题。 datetime2 does not allow DateTime.Min but DateTime does. datetime2不允许DateTime.Min,但DateTime不允许。 The DateTime.min value is 00:00:00.0000000, January 1, 0001. DateTime.min值是0001:00年1月1日00:00:00.0000000。

Link with details. 链接详细信息。

Conversion of a datetime2 data type to a datetime data type results out-of-range value 将datetime2数据类型转换为日期时间数据类型会导致超出范围的值

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. 我们可能已经错过了CreatedDate之外的非可空DateTime属性,并且没有在Seed方法中指定。
  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. 非常容易忘记:如果您使用自定义属性创建自定义AspNet.Identity实体,您可能在其中放置了一个不可为空的DateTime属性,就像我的情况一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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