IT:EF/CodeFirst/Migrations:HowTo:Strategies
Summary
Understanding the changes to workflow that arise due to using CodeFirst Migrations can be a challenge at first.
Process
- Invoke Enable-Migrations with
-AutoMigrationsturned on, in order to start the migration with no InitialMigration in the Migrations Folder. - If:
- you'll be applying migrations to a database structure that will be created by other means (ie by an imported initial script): invoke Add-Migration with
-IgnoreChangesin order to create a blank InitialMigration.- If you don't, when running Migrations, the InitialMigration will try to create already existing tables, and cause an exception.
- if your script will be creating the database, then just call Add-Migration to create an InitialChanges that creates the tables.
- Once the InitialChanges migration is created, Disable Automatic Migrations so that you have to always create Coded Migrations.
- Whether you were using a DbContext initialiser that was dropping/recreating the database or not, switch to a Migrations based initialiser.
- If the db changes, it will ensure that a coded migration is created, then applied (no need to manually call Update-Database.