Entity Framework Code First – from development to production

Normally your development environment has no connection tot the live environment … well, it shouldn’t have. You develop in a development environment.

When using migrations in a code first approach, you will probably not be able to update the database on the production server (or staging server).

Luckily there is a simple way to get your database changes in a staging / production environment, simply by generating SQL scripts.

To generate a full script, in case you need to generate the database starting from zero, in Visual Studio’s Package Manager Console, enter:

update-database -script -SourceMigration:0

This will generate a SQL-script for all migrations.

Once the database is in production, after you have made changes, you can generate a change scripts of a single migration using:

update-database -script -SourceMigration:[name of the migration]

When entering the name of the migration, you do not need to enter the datestamp-portion of the name.

To generate change scripts of multiple migrations (in sequential order), enter:

update-database -script -SourceMigration:[name of the migration] -TargetMigration:[name of the migration]

Once you have the scripts you can test them and after that run them on your production database.

Leave a Reply