Debug code-first Entity Framework migration codes

后端 未结 7 736
悲&欢浪女
悲&欢浪女 2020-11-27 10:16

I\'m using Entity Framework code first in my website and I\'m just wondering if there is any way to debug the migration codes. You know, like setting breakpoints and stuff l

7条回答
  •  攒了一身酷
    2020-11-27 10:59

    To hit a break point in a db migration set the context to MigrateDatabaseToLatestVersion on initialise.

    Database.SetInitializer(new MigrateDatabaseToLatestVersion());
    

    Then you just debug as normal (run using f5) and the breakpoint will hit the first time you run the project.

    The problem now is that if you debug a second time the migration will not run. This is because the __MigrationHistory table has been updated to say you have migrated to the latest version. To re-test the migration open the package manager console and downgrade to the previous migration:

    Update-Database –TargetMigration: ThePreviousMigrationName
    

提交回复
热议问题