nishihara.me


navigation
home
twitter
github
flickr
about
I am a developer and photography enthusiast.

How to change the namespace of an Entity Framework (EF) DbContext on Asp.Net

10 Sep 2015

Sometimes you need to change the namespace of a project due to refactoring or project name change. When you are working with Entity Framewok DBContext on Code First approach and Migrations enabled, if you change the namespace it may be thrown some exceptions, like There is already an object named '[name of object]' in the database. and Failed to set database initializer of type 'System.Data.Entity.MigrateDatabaseToLatestVersion..

There are some steps you have to make when changing the namespace of DBContext on an existing database:

  1. Set the ContextKey on DbMigrationsConfiguration;
  2. Update the ContextKey column on _MigrationHistory table of the database.

The EF default ContextKey with Migrations enabled is the class which inherits DBContext plus its namespace , i.e, if your namespace is MyMusicStore.Models and the class which inherits DBContext is MusicContext, then its default ContextKey is MyMusicStore.Models.MusicContext.

This same ContextKey is used on database“s _MigrationHistory table to track changes on your models.

When you change the namespace, you change the ContextKey together, therefore, on the first call to DBContext the ContextKey from the application will not match the ContextKey column from the Database and it will throw an exception.

Set the ContextKey on DbMgrationsConfiguration

If you are using the default DbMigrationsConfiguration, set the ContextKey as follows:

DbMigrationsConfiguration config = new DbMigrationsConfiguration();
config.ContextKey = "MyMusicStore.Models.MusicContext";
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MusicContext,config>());

Particurlarly, if your setup includes Owin + EntityFramework, add the above code on Configuration method from the OwinStartup class.

Update the ContextKey column on _MigrationHistory table of the database

Connect to the SQL Server Database and update the ContextKey column from the _MigrationHistory table, as follows:

UPDATE [dbo].[__MigrationHistory]
   SET [ContextKey] = 'MyMusicStore.Models.MusicContext'
 WHERE [ContextKey] = 'MyOldNamespace.MusicContext'
GO

Summary

If you change the Entity Framework DbContext namespace make sure that the ContextKey from the DbMigrationsConfiguration and _MigrationHistory table are the same.

Resource: