简体   繁体   中英

Entity Framework: Add-Migration fails with Unable to update database

I have been using Entity Framework (5.0) for a while now in a project (ASP.NET MVC in VS2012 Express). Right now, though, I am no longer able to add migrations.

PM > Add-Migration -projectName MyProject.DAL TestMigration
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

I do not know if this gives any clue but the 'Unable to ..." text is displayed in red.

I have tried to enable automatic migration (which makes no sense as I am trying to write the pending model changes to a code-based migration) and that results in the required migration in the database. However this is not what I want because I then I do not have a migration in the project.

I have tried to remove the database and recreate the database. The database is recreated (up to the previous migration) but when I then try to use the Add-Migration I still get the "Unable to update.." error.

Edit

I tried the -force parameter but with no difference.

The contents of my configuration class (I did not change anything after the previous migration):

    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(Bekosense.DAL.Context.BekosenseContext context)
    {           
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageDrop);
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageCreate); 
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentDrop);
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentCreate); 
        context.Database.ExecuteSqlCommand(Properties.Resources.AddDbUsers);
    }

Edit 2 I found out that I am able to do an add-migration when I comment the following line out in my DbContext:

//Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());

when I leave the line above active and comment out everything in the Configuration file, it still won't work. Why is the Database.SetInitializer line causing this strange behaviour?

You can reset the entity framework to solve your problem [ But keep it mind it will bring the Migration to the default state ]

Note: To take a backup before performing the following

You need to delete the present state:

  • Delete the migrations folder in your project
  • Delete the __MigrationHistory table in your database (may be under system tables)

You will find the __MigrationHistory table in your database [Under App_Data Folder]

Then run the following command in the Package Manager Console:

Enable-Migrations -EnableAutomaticMigrations -Force

Use with or without -EnableAutomaticMigrations

And finally, you can run:

Add-Migration Initial

This may also help you

Never use automigrations, that gave me problems in the past (when migrating the database down, use the correct way to do it from the start!)

This line should be in your global.asax:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());

And not in your DbContext!

Other tips if the above won't help:

Perhaps you have multiple layers in your application:

Add-Migration 000  -StartupProjectName "NameOfTheProjectInSolutionExplorer" -ConfigurationTypeName "MyConfiguration"  -ConnectionString "theconnectionstring;" -ConnectionProviderName "System.Data.SqlClient" -Verbose

Above is the Add-Migration command i use for a multi-layered application.

Same thing for an update of the database

Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -script

In my case I've got the same error because I was forcing ObjectContext.CommandTimeout on class DbContext at constructor method during migration.

Try removing it

((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 5000;

This worked for me:

update-database -targetmigration:"0" -force -verbose
add-migration Initial
update-database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM