Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 228
dreza Avatar asked May 12 '15 09:05

dreza


People also ask

How does migration work in Entity Framework?

Migration is a way to keep the database schema in sync with the EF Core model by preserving data. As per the above figure, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model.

What is MVC migration?

The Migrations feature enables you to change the data model and deploy your changes to production by updating the database schema without having to drop and re-create the database.

How do I get rid of migrations in Entity Framework?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

How do I rollback migration in Entity Framework Core?

To revert the last applied migration you should (package manager console commands): Revert migration from database: PM> Update-Database <prior-migration-name> Remove migration file from project (or it will be reapplied again on next step) Update model snapshot: PM> Remove-Migration.


2 Answers

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'
like image 170
Paul-Jan Avatar answered Nov 03 '22 03:11

Paul-Jan


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...

like image 30
coolboyjules Avatar answered Nov 03 '22 01:11

coolboyjules