Unable to generate an explicit migration in entity framework

后端 未结 28 1224
情书的邮戳
情书的邮戳 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:59

    I had the same problem. Apparently entity framework generates this error when it's unable to connect to the database. So make sure that you're able to access it before searching for other problems.

    0 讨论(0)
  • 2020-12-13 00:00

    This error means there are pending migrations need to be commited before you can execute another explicit migration. You can choose to

    1. Execute those pending migrations using Update-Database command
    2. Delete those pending migrations. Safest way is open Migrations folder, right click on [201203170856167_left] > Exclude from project

    After this one you can start "Add-Migration ..." again

    Hope it helps

    0 讨论(0)
  • 2020-12-13 00:00

    I also came across this issue. It came when I created new DB and I had pending changes for my code-first DB migration then I tried to run "Update-Database" command. Solution : Run "Add-Migration -MigrationName" command to create new migration for new DB. Then run "Update-Database" command.

    0 讨论(0)
  • 2020-12-13 00:02

    Historically I always solved this by deleting the pending migrations, or if there was only 1 remaining and it was mostly desirable, by using -f to recreate it.

    Recently, this has stopped working for me.

    When this happened the first time, I restarted Visual Studio, and then it let me proceed.

    The second time, it only worked after I ran a Clean on the project. It was almost as if the pending migrations were retained despite deleting all the files from explorer.

    0 讨论(0)
  • 2020-12-13 00:05

    1. Connection String / Connection Permissions

    Check the connection string again.

    Make sure the user you are connecting with still has permission to read from [__MigrationHistory] and has permission to edit the schema.

    You can also try changing the connection string in the App or Web config file to use Integrated Security (Windows Auth) to run the add-migration command as yourself.

    For example:

    connectionString="data source=server;initial catalog=db;persist security info=True;Integrated Security=SSPI;" 
    

    This connection string would go in the App.config file of the project where the DbContext is located.

    2. StartUp Project

    You can specify the StartUp project on the command line or you can right click the project with the DbContext, Configuration and Migrations folder and select Set as StartUp project. I'm serious, this can actually help.

    0 讨论(0)
  • 2020-12-13 00:05

    For me, i deleted the migration file(in your case "201203170856167_left") from Migrations folder, and then ran the below command in Package Manager console

    Add-Migration <Parameter>
    Update-Database
    
    0 讨论(0)
提交回复
热议问题