Database Change Control, Part 4

We covered Entity Framework Code First in an earlier post where we just let Entity Framework create the database tables for us from a schema. That pattern might break down when we have actual data, but I do think letting Entity Framework create your tables is a great pattern for proof on concepts.

In this post we are going still use Entity Framework but now we will use migrations, which will give us much of the same power as DbUp but more of a C#-ish feel. With DbUp, you write your migrations in SQL. With Entity Framework, you can write migrations in C# and retain the power of doing change based-migrations.

To get started with migrations you need to fire up the “Package Management Console” within Visual Studio and run this command:

Enable-Migrations -EnableAutomaticMigrations

That will create a Migrations directory.

From the Package Management Console, run:

Add-Migration TimeLogItems

That will create a new migration in the Migrations folder.

Now you just need a simple migration to create the TimeLogItems table.

Now to push these changes to a database just run this command:

Update-Database

With this in place making future changes is super easy, just add more migrations.


Related posts