EF 6 Code First Migrations not executing Seed method if I configure use of datetime2

Viewed 63

I am using Entity Framework database migrations for the first time. It's been a learning experience, but I've got everything working. Almost. For some reason, when I specify that I want columns created as datetime2 rather than datetime, the Seed method is no longer called.

In MyDbContext.cs, I specify MigrateDatabaseToLatestVersion, like so:

static MyDbContext()
{
    System.Data.Entity.Database.SetInitializer(
            new System.Data.Entity.MigrateDatabaseToLatestVersion<MyDbContext, MyDbConfiguration>());
}

In MyDbConfiguration.cs, I have a Seed method:

protected override void Seed(RightToKnowDbContext context)
{
    context.Counties.AddOrUpdate(
        p => p.CountyFips, 
            new County { CountyFips = "001", CountyText = "Adams" },
            new County { CountyFips = "003", CountyText = "Alexander" },
            [...]
            new County { CountyFips = "201", CountyText = "Winnebago" },
            new County { CountyFips = "203", CountyText = "Woodford" }
    );
}

When I execute the application the first time, it creates the tables in my database and seeds the Counties table with data, just as desired.

However, I would like it to create columns as datetime2 rather than as datetime. To do this, I add this line to the OnModelCreating method in the MyDbContext class:

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

After making that change, I rollback all migrations in my database to get a fresh start, then run the application for the first time. Just as before, it creates all of the tables, only this time with datetime2 columns. However, it does not run the Seed method (I have confirmed this in the debugger), so the Counties table does not get populated with data. Why?

I have tried numerous variations on this, each time rolling back the database, making a change, then running the application and checking the result. Each time, if I don't have the datetime2 configuration line, the Counties table is seeded with data, but if I put the line back, it is not.

One thing I've noticed is that, with the datetime2 configuration in place, after it runs the initial migration, it immediately runs another migration called "202206021857358_AutomaticMigration". My guess is that perhaps it is creating the tables with normal datetime columns during the initial migration, then altering the table to use datetime2 columns instead, and as a result it is for some reason skipping the Seed method.

Is there a way to fix this? I realize I could just manually do 'Update-Database' from the Package Manager Console and that would probably avoid the problem, but that's a manual step that I would need to remember to do for each database server (dev, test, QA, and production). I'll do that if I have to, but I would much prefer to use the automated method.

This is Entity Framework 6 (not Core), .Net Framework 4.7.2, and SQL Server 2019.

0 Answers
Related