Best practice for using entity framework for post deployment database changes

asp.net, ef-database-first, entity-framework

Solution

You should use Code First, so that you can use Migrations.

More specifically I'd use manual Migrations.

With manual Migrations you can create Migrations at any point in time. A migration has the following information:

- a snapshot of the current model

- a set of "instructions" to upgrade from the previous migration

- a set of "instructions" to downgrade to the previous migration

Apart from the necessary Migrations, you should add a new Migration when you deploy your app. For example, you can create a migration called "Version 1.0", when you deploy the version 1.0 of your app.

When you finish each new stable version, you simply add a new migration, for it, like "Version 1.1" or "Version 1.2".

The interesting part of migrations shows up when you have a deployed application version and you need to upgrade (or downgrade) to a new version.

There are commands that let you upgrade (or downgrade) a DB from one particular version to another particular version. You can do it directly specifying a connection to the DB, or create a SQL script which will apply the changes to the DB. For example, if you deployed the version 1.0 in a customer server, and you need to upgrade the software to version 1.2, you can do this:

Update-Database -SourceMigration "Version 1.0" -TargetMigration "Version 1.2" 
      -Script

This will create a SQL script which can be run on the DB to upgrade from version 1.0 to 1.2.

If you need help about any of the Migrations commands, simply type:

get-help Update-Database -full

(`Update-Database` is a command name, you can specify any other like `Add-Migration`)

It's possible that you need to specify the project where the model is in, the connection string name, the name of the project with the .config file or some other things, depending on the specified parameters, and the structure of the projects in your solution.

To get more info on migrations, read MSDN EF Code First Migration.

NOTE ADDED IN EDITION: there is a new DB initializer that can automatically migrate to the latest version when the application runs. I've worked in a real application, and it works like a charm.

ALTERNATIVE SSDT

If you don't want to follow the advice, you can use SQL Server Data Tools (which can be installed inside VS, or work as an independente application, depending on the version you're using).

The idea of this tool is that you can compare projects (which are DB schema snapshots) to existing DBs, and generate the scripts to change the DB to match the schema in the project. (In fact you can compare any combination ofr project and DB)

If you keep versions of your project in a CVS you can even check the changes from one verison of the project to other version of the project.

NOTE ADDED IN EDITION: a SSDT project is a set of scripts that can build the whole DB, including all the objects in its schema. You can create it from an existing DB or viceversa. Then you can keep comparing any combination of DBs and SSDT projects, as soruce or target, and create an apply the scripts neccesary to change each particular object which has changes. (The scripts are created to change the target so that it looks like the source, but you can swap source and target easyly)

This is an alternative solution, but Migrations are much more powerful because you can customize them, for example to fill a "master table" when creating it, to set the initial value of a new column, and so on. If you use SSTD you'll have to do all of that by hand, and carefully keep track of it. So I highly recommend using Migrations.

Problem

I am working on a ASP.NET Web Forms project which is expected to experience lots of database changes even after deployment. Our preference was to use Entity Framework 5, and the Database First design paradigm. However, since we have to make lots of changes to the database even after deployment, if I use database first approach, then every time I update my database, I will have to delete my entire model and regenerate it. Is there any best practices to make this process less painful?

Original source