Entity Framework And Namespace Refactoring

18 Jun 2020 - Sanjeev

As the time goes by, few Namespaces used in project(s) no longer make sense. It is better to refactor such Namespaces to some meaningful ones. Namespace refactoring usually should not be a problem. Surprise is waiting if entity framework is used in the project.

This is specifically true if the project uses Entity framework(v6.0)’s Code first migration. This can be confirmed by checking the schema of __MigrationHistory table. If this table has a column by name ContextKey then EF code uses Namespace.

If entity framework’s migtation is using Namespace and there is a need to for refactoring the Namespace, then below solution would work for a typical project. Here typical project mean, a project with single DBContext. Simple trick is to drop the column ContextKey. With this, when the migration is run next time, Entity framework will auto re-create the column ContextKey with new Namespace. Below SQL can be used to delete the column

    
        ALTER TABLE __MigrationHistory DROP PRIMARY KEY;
        ALTER TABLE __MigrationHistory DROP COLUMN ContextKey
        ALTER TABLE __MigrationHistory ADD CONSTRAINT PK_dbo.__MigrationHistory PRIMARY KEY (MigrationId)
    



Do let me know if you have any clarification/suggestion on this post. I will be happy to know your feedback!