Debugging Package Manager Console Update-Database Seed Method

前端 未结 7 778
北恋
北恋 2020-12-07 12:53

I wanted to debug the Seed() method in my Entity Framework database configuration class when I run Update-Database from the Package Manager Console

相关标签:
7条回答
  • 2020-12-07 13:43

    A cleaner solution (I guess this requires EF 6) would IMHO be to call update-database from code:

    var configuration = new DbMigrationsConfiguration<TContext>();
    var databaseMigrator = new DbMigrator(configuration);
    databaseMigrator.Update();
    

    This allows you to debug the Seed method.

    You may take this one step further and construct a unit test (or, more precisely, an integration test) that creates an empty test database, applies all EF migrations, runs the Seed method, and drops the test database again:

    var configuration = new DbMigrationsConfiguration<TContext>();
    Database.Delete("TestDatabaseNameOrConnectionString");
    
    var databaseMigrator = new DbMigrator(configuration);
    databaseMigrator.Update();
    
    Database.Delete("TestDatabaseNameOrConnectionString");
    

    But be careful not to run this against your development database!

    0 讨论(0)
提交回复
热议问题