How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer?

前端 未结 15 908
-上瘾入骨i
-上瘾入骨i 2020-12-07 19:39

I\'m using the DbContext and Code First APIs introduced with Entity Framework 4.1.

The data model uses basic data types such as string

15条回答
  •  误落风尘
    2020-12-07 20:24

    One line fixes this:

    modelBuilder.Properties().Configure(c => c.HasColumnType("datetime2"));
    

    So, in my code, I added:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Properties().Configure(c => c.HasColumnType("datetime2"));
    }
    

    Adding that one line to the DBContext subclass override void OnModelCreating section should work.

提交回复
热议问题