EntityFramework – SQLException during database migration

Recently I’ve been doing some small refactoring of my pet project. Nothing too fancy – just namespace adjustment, variables and class renaming etc. However it turned out that even these minor changes can in fact be major ones. All of the sudden most of my integration tests started failing due to SQLException.
I am using Code First approach so I didn’t take me too much time to realize that for some reasons EntityFramework wanted to create already existing tables. I started looking into the way EF keeps track of the changes in the model and it turned out that it creates special table called

One of the most important columns in there is column called ContextKey, which identifies the class which describes migration policy. In my case this class originally looked that way

In that case value of the ContextKey column matches the full name of the class

The class after refactoring was slightly changed and now it looks that way

It is easy to overlook the change but now, full name of the class is

so EF loses the information about the migration and tries to migrate once again from the scratch. Fortunately there is an easy fix for that which doesn’t require dropping the database. All you have to do is to write simple update statement which set the proper ContextKey value.

Source code for this post can be found here

EntityFramework – SQLException during database migration