IT:AD:EF/CodeFirst/Migrations:HowTo:Add-Migration/Initial Migration
Summary
If you invoked IT:AD:EF/CodeFirst/Migrations:HowTo:Enable-Migrations with -EnableAutomaticMigrations (so that no InitialCreate migrations was created), when you turn on Turn off Automatic migrations (in the Migrations/Configuration.cs file), you probably want to create a first migration to get things going.
Use this method to create a blank Migration as a starting point (we want a blank, as the db is already created, which would mean that if we had an InitialCreate that defined all the tables, it would crash trying to build tables that already exist).
Process
Use:
-IGNORECHANGESScaffolds an empty migration ignoring any pending changes detected in the current model. This can be used to create an initial, empty migration to enable Migrations for an existing database.
N.B. Doing this assumes that the target database schema is compatible with the current model.
Add-Migration NewNameForMigration -startupprojectname:App.AppHost.Web -projectname:App.Infrastructure -IGNORECHANGES -verbose -debug
The output
The Migration it will create is really just a marker:
public partial class InitialCreate : DbMigration {
public override void Up()
{
}
public override void Down()
{
}
}
Run Update-Database
Now that you have a Migration, run Update-Database
This will do two things. It will execute the Up method in the initial migration, which has no effect on the database.
And it will create the Migration-History table in your database based on the current state of the model.
That way, if you are using automatic migrations, next time Code First does db initialization, it will compare the current model to the one stored in the migration-history to determine if the database needs to be migrated/updated.