Entity Framework Code First - Initial Code Migration Not Working

I am trying to perform migrations from code. I created my models and included-migrations and then added an initial migration, this initial migration contains all the create tables, etc.

public partial class Initial : DbMigration
 {
     public override void Up()
     {
        CreateTable(..........
     }

 public override void Down()
    {
           DropTable(...........
     }
}

      

Then I tried the Update-Database command from visual studio which works great, creates the database and runs the initial migration.

Then I will drop the database from Sql Studio. Then I run a console application that calls the Migration Manager class

// MigrationManager class

    public static bool PerformMigration(string migrationId)
    {
        bool success = false;
        try
        {
            DbMigrationsConfiguration<MyDbContext> config = new Configuration();
    ....
            DbMigrator migrator = new DbMigrator(config);
            migrator.Configuration.AutomaticMigrationsEnabled = false;
            if (string.IsNullOrEmpty(migrationId))                    
                migrator.Update(); --> fails saying pending migration
            else
                migrator.Update(migrationId);

            success = true;
        }
        catch (Exception e)
        {
            success = false;
            LastException = e.Message;
        }

        return success;
    }

      

Update () failed with the following error:

The database cannot be updated to match the current model because there are pending changes and automatic migration is disabled. Either write pending model changes to a code-based migration or enable automatic migrations. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

//Configuration.cs
internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(WorkflowConfigurationDbContext context)
    {
        SeedData(context);
    } 
    private void SeedData(){...}    

}

public class MyDbContext : DbContext
{
    public MyDbContext()
    {            
    }        

    public DbSet....

    }

      

When I go to the Update () call, it goes into the Configuration () constructor and the MyDbContext () constructor, but after that doesn't work, it seems like it doesn't try the Primary Migration at all.

+3


source to share


2 answers


Make sure your database initialization strategy is correct in your EF context constructor, for example:

 public partial class YourContext: DbContext
 {
     static YourContext()
     {
         Database.SetInitializer<YourContext>(new CreateDatabaseIfNotExists<YourContext>());
     }
 }

      



This is done the first time the database is accessed.

EDIT: The second issue might be security related: does the user performing the migration have the required permissions?

0


source


Found the problem, it was the wrong namespace.



DbMigrationsConfiguration<MyDbContext> config = new Configuration();
config.MigrationsNamespace = "Correct Namespace";

      

0


source







All Articles