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
1 |
__MigratoinHistory |
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
1 2 3 4 5 6 7 8 9 10 11 |
namespace EFAutoupgradeException.Data { public class AutoupgradeExceptionMigrationConfiguration : DbMigrationsConfiguration<AutoupgradeExceptionDbContext> { public AutoupgradeExceptionMigrationConfiguration() { this.AutomaticMigrationsEnabled = true; this.AutomaticMigrationDataLossAllowed = true; } } } |
In that case value of the ContextKey column matches the full name of the class
1 |
EFAutoupgradeException.Data.AutoupgradeExceptionMigrationConfiguration |
The class after refactoring was slightly changed and now it looks that way
1 2 3 4 5 6 7 8 9 10 11 |
namespace EFAutoupgradeExceptions.Data { public class AutoupgradeExceptionMigrationConfiguration : DbMigrationsConfiguration<AutoupgradeExceptionDbContext> { public AutoupgradeExceptionMigrationConfiguration() { this.AutomaticMigrationsEnabled = true; this.AutomaticMigrationDataLossAllowed = true; } } } |
It is easy to overlook the change but now, full name of the class is
1 |
EFAutoupgradeExceptions.Data.AutoupgradeExceptionMigrationConfiguration |
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.
1 2 3 4 5 |
USE EFAutoupgradeException UPDATE [__MigrationHistory] SET [ContextKey] = 'EFAutoupgradeExceptions.Data.AutoupgradeExceptionMigrationConfiguration' WHERE [ContextKey] LIKE '%AutoupgradeExceptionMigrationConfiguration' |
Source code for this post can be found here