Friday, October 21, 2016

Performing Entity Framework Code First Migrations step-by-step

If you need to perform Entity Framework Code First Migrations, you will probably end up following this article:

However, as with most Microsoft articles, the documentation leaves a lot to be desired...

Therefore, I will provide a more thorough step-by-step walk through on just how to do this!

  1. After opening up the Package Manager Console, you will want to make sure you choose the "Default project" before running the command to "Enable-Migrations"

  2. This will then create a Configuration.cs file shown as follows:

  3. Next, when you run "Add-Migration <MyMigrationName>", then you will get the following message along with the migration file added to your project:

  4. You can then run the "Update-Database" command if you want to run the command directly against the database. However, if you are like most developers and want to know what the database script looks like before it is applied to the database, you can instead run the "Update-Database -Script" command which will generate a SQL script instead which you can then save off for reference!

  5. For every change each developer makes to the Entity Framework Code First models, you simply repeat the "Add-Migration" and "Update-Database" steps respectively (corresponding to the Code First model changes that were made) and you can ignore the "Enable-Migrations" command.  That is all there really is to it! :-) 

No comments:

Post a Comment