Thursday, July 19, 2012

[C# and EF] Tutorial: Entity Framework Code First Migrations 2/2

This second part of the series shows how to activate EF Code First Migrations and either handle them manually in code or – even more interesting - letting EF handle them automatically for you.

Manual Code-based EF Code First Migrations

The first possibility is to handle those changes manually from within your code. Here is how that works.

  • Open the Package Manager Console from the Tools menu within Visual Studio 2012.

image_thumb2

  • Activate the manual EF Migration features by entering the command “Enable-Migrations”.

image_thumb4

  • This adds a new folder Migrations and also the auto-generated classes Configuration.cs  and [SOMEDATE]_InitialCreate.cs to your project.

image_thumb5

  • The Configuration.cs class allows you to configure the EF Code First Migrations options and seed data after migration (very useful for testing purposes during development).

image

  • The other class contains all the code necessary to create the database (Up) and also drop it (Down) if necessary.

image

  • Here is an example of what can be found in the Up() method.

image

  • Here is an example of what can be found in the Down() method.

image

From here you may start to implement you own code in C# for handling EF Code First and database schema changes.

Automatic EF Code First Migrations

Being able to handle database schema changes related to EF Code First changes from C# code is great, you won’t need to be an expert of database development anymore. But you still need to know what to change and you still need to allocate some time for it.

What if there would be a fully automated way of handling those changes without any effort? Well there is and you just have to activate it. Here is how that works.

  • Open the Package Manager Console from the Tools menu within Visual Studio 2012.

image_thumb2

  • Activate the automatic EF Migration features by entering the command “Enable-Migrations -EnableAutomaticMigrations”.
  • If you try that on a project where you have already activated the manual EF Code First features, you just need to delete the Migrations folder first.

image

  • If you look into the Configuration.cs file you will see that the flag to activate automatic migrations is now enabled.

image_thumb7

  • When you now change the EF Code First model as explained in the first post of the series, you may start the automatic migration of the database via the Package Manager console by entering “Update-Database”.

image

  • And you see that the automatic migration was not applied because it would result in data loss. By default it is not allowed to apply automatic migrations if there is data loss implied. This is a security feature to not accidently delete important data from the database. Mind that you may revert back to an old database schema but you won’t get back deleted data!
  • So now it is up to you to either review the changes or allow automatic migrations if there is data loss (really use this with percaution!!).
  • To allow data loss you just set the flag AutomaticMigrationsDataLossAllowed to true in the Configuration constructor.

image

  • When you restart the automatic migration of the database via the Package Manager console by entering “Update-Database” (you may want to add the “–Verbose” flag) you see all the changes that were applied.

image

  • You can now use your application as expected. The modifications in the database reflect the EF Code First model changes.

Share/Save/Bookmark

2 comments:

Anonymous said...

Best explanation of Migrations I've seen yet. Very well written.

Jason De Oliveira said...

Thank you very much. I am happy that you find it useful.