Entity Framework code-first migrations

This post is just a little reminder for myself on the syntax for common Entity Framework operations. To be executed in the package manager console in Visual Studio.

Enable migrations in project:


This will add the InitialCreate migration reflecting the current state of the database.

Add new migration after making changes to a model:

Add-Migration MyMigrationName

Update the local database to include any new migrations:


Get SQL script for applying any migrations not applied in the local database, to use for example when applying the changes in a production database:

Update-Database –Script

Get SQL script to apply changes from one specific migration and another, if all migrations already has been applied locally and you want to make changes to a production database:

Update-Database -Script -SourceMigration: MigrationX -TargetMigration: MigrationY

If you stumble on problems with getting the right connectionString use thi:

Update-Database -Script -StartUpProjectName: MyProjectName -SourceMigration: MigrationX -TargetMigration: MigrationY