Entity Framework Code First Migrations

I assume you’ve created a project, code first model to work with, and installed the latest version of Entity Framework NuGet package.

After you’ve built an Initial Model and Database, you need to enable migrations: run the

Enable-Migrations

command in Package Manager Console.

Next you’re going to become familiar with two primary commands: Add-Migration and Update-Database. Example:

Add-Migration AddFieldToModel
Update-Database

Both commands are run in Package Manager Console.

So far we’ve always upgraded to the latest migration, but there may be times when you want to upgrade/downgrade to a specific migration. Imagine you’ve created a Migration called DeleteFieldFromModel and you want to migrate it. You can use the -TargetMigration switch to downgrade to this migration. Example:

Update-Database -TargetMigration:DeleteFieldFromModel

If you want to roll all the way back to an empty database then you can use the

Update-Database -TargetMigration:$InitialDatabase

command.

Source: Entity Framework Code First Migrations