Moving Entity framework to another project from MVC causes re-migration

爷,独闯天下 提交于 2019-12-07 03:27:33

问题


I currently have an asp.net MVC 4 application which contains Entity framework 6 Code First models, DbContext and Migrations. In an attempt to separate this from my web application so I can re-use these database classes in another project I have moved all related Entity Framework classes to their own project.

However now when I run the solution it thinks my model has changed and attempts to run all my migrations once more. The problem appears to be in my use of SetInitializer as if I comment out this line I can run the web application as per normal.

public static class DatabaseConfig
{
    public static void Initialize()
    {
       System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<G5DataContext, Configuration>());

        // make sure the database is created before SimpleMembership is initialised
        using (var db = new G5DataContext()) 
            db.Database.Initialize(true);
    }
}

This wasn't a problem until I've tried to move all the Entity Framework classes. Is this not possible, or have I done something fundamentally wrong?


回答1:


At startup, EF6 queries exiting migrations in your database, as stored in the __MigrationHistory table. Part of this table is a context key, which includes the namespace of the entities.

If you move everything to a new namespace, EF6 doesn't recognize any of the previously run migrations, and tries to rebuild the database.

A quick solution is to run a script to rename the context key in the __MigrationHistory table to your new namespace. From http://jameschambers.com/2014/02/changing-the-namespace-with-entity-framework-6-0-code-first-databases/ :

UPDATE [dbo].[__MigrationHistory] 
   SET [ContextKey] = 'New_Namespace.Migrations.Configuration'
 WHERE [ContextKey] = 'Old_Namespace.Migrations.Configuration'



回答2:


Would also like to add that you should remember to change the ContextKey property in your Configuration class. I did the above but it was still trying to create a new database. Here's an example:

Before:

internal sealed class Configuration : DbMigrationsConfiguration<PricedNotesContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        ContextKey = "Synapse.DAL.PricedNotesContext";
    }

    protected override void Seed(PricedNotesContext context)
    {
    }
}

After:

internal sealed class Configuration : DbMigrationsConfiguration<PricedNotesContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        ContextKey = "SynapseDomain.DAL.PricedNotesContext";
    }

    protected override void Seed(PricedNotesContext context)
    {
    }
}

Hope this helps anyone who is stuck on this. It's a shame that it shouldn't be easier...



来源:https://stackoverflow.com/questions/30187115/moving-entity-framework-to-another-project-from-mvc-causes-re-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!