Posted with : Entity Framework, Cloud Platforms

EntityFramework - migrate to latest version on Azure and local database from code

When working with EF Code First approach, we must update system database after modified model entities and of course, we must create a migration (or many migrations, depending on how are your changes). Later we can update the database manually or automatically (on the first read/write to the database).

Upgrade a local database

After the model has been changed according to the requirement, we can build the application without any error but when run the app it will raise a runtime error as below (great one :))


Oh, what was wrong? The system database is not equivalent to model. An update action is needed, we can do it simply as below:



OK, it works as expected now locally. But if the database is on the cloud, how to proceed?

Upgrade a cloud database

Following this guideline on site (section Deploy to Azure), we do a manual update step. It requires VS version >= 2013. Oh what happens it our tool does not neet this requirement? Even more, how to proceed if we want to have an automatic way?

The answer is pretty simple, there is a strategy in EF we can use out-of-the-box MigrateDatabaseToLatestVersion


On the first db read or write, EF will perform the migration task for us. It is useful also in case the team follows continuous integration strategy, no manual step is needed.


  1. Code First Migrations
  2. Running & Scripting Migrations From Code
Written on August 30, 2015