Unable to generate an explicit migration in entity framework

后端 未结 28 1308
情书的邮戳
情书的邮戳 2020-12-12 23:31

I am adding a new migration but this message shows:

Unable to generate an explicit migration because the following explicit migrations are pending:

相关标签:
28条回答
  • 2020-12-12 23:41

    If you haven't used Update-Database you can just delete it. If you've run the update then roll it back using Update-Database -TargetMigration "NameOfPreviousMigration", then delete it.

    Reference: http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/

    I copied this text directly from here: How do I undo the last Add-Migration command?

    0 讨论(0)
  • 2020-12-12 23:42

    I had the same problems and was only able to resolve it running Add-Migration 'MigrationName' -Force

    With -Force being the important part.

    0 讨论(0)
  • 2020-12-12 23:42

    Tip: It's always good to use the -Script switch for migration commands if you're not sure. It also really helps understand what Update-Database actually does.

    I run the following to update the database, then I get a script I can apply manually (or just run it again without the -Script tag).

    For Update-Database I would run the following :

    Update-Database -Script -ConfigurationTypeName Configuration_ASPNETIdentity -ConnectionStringName SQL_AzureLive

    Where SQL_AzureLive is the named connection string in my config.

    Then I can verify the SQL looks right, apply it and be done. As many others have said if the connection string is wrong or invalid you'll get this error.

    0 讨论(0)
  • 2020-12-12 23:43

    It tells you that there is some unprocessed migration in your application and it requires running Update-Database before you can add another migration.

    0 讨论(0)
  • 2020-12-12 23:44

    Scenario

    • I am working in a branch in which I created a new DB migration.
    • I am ready to update from master, but master has a recent DB migration, too.
    • I delete my branch's db migration to prevent conflicts.
    • I "update from master".

    Problem

    After updating from master, I run "Add-Migration my_migration_name", but get the following error:

    Unable to generate an explicit migration because the following explicit migrations are pending: [201607181944091_AddExternalEmailActivity]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

    So, I run "Update-Database" and get the following error:

    Unable to update database to match the current model because there are pending changes and automatic migration is disabled

    Solution

    At this point re-running "Add-Migration my_migration_name" solved my problem. My theory is that running "Update-Database" got everything in the state it needed to be in order for "Add-Migration" to work.

    0 讨论(0)
  • 2020-12-12 23:47

    In my case (using MS Visual Studio), it was as simple as restarting Visual Studio.

    0 讨论(0)
提交回复
热议问题