Unable to generate an explicit migration in entity framework

Entity FrameworkEntity Framework-Migrations

Entity Framework Problem Overview


I am adding a new migration but this message shows:

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

Can any one help me?

Entity Framework Solutions


Solution 1 - Entity Framework

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

Solution 2 - Entity Framework

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.

Solution 3 - Entity Framework

You either need to run "update-database" from the package manager console to push your changes to the database OR you can delete the pending migration file ([201203170856167_left]) from your Migrations folder and then re-run "add-migration" to create a brand new migration based off of your edits.

Solution 4 - Entity Framework

This error can also mean that the migrations are not recognized anymore. This happened to me after having changed the value of the ContextKey in Migrations.Configuration. The solution was simply to update the ContextKey in the database table "__MigrationHistory" (or revert the value in the Configuration class I guess). The ContextKey and the Namespace in your application should match.

Solution 5 - Entity Framework

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.

enter image description here

Solution 6 - Entity Framework

Had the same issue and was able to solve with some hints from above answers:

  • In package manager console check the default project (point to the project with the migration configuration
  • Ensure the startup-proj has a web.config with a valid connectionstring ( or
  • Ensure the project with migrations has an app.config / web.config with a valid connectionstring
  • Check permissions in DB (for the user configured in you connectionstring)

Use "update-database -verbose" in the package manager console to get more specific information where migrations tries to connect to. (Helped in my case to find out my startup proj was not set correctly...)

Solution 7 - Entity Framework

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: https://stackoverflow.com/questions/21312103/how-do-i-undo-the-last-add-migration-command

Solution 8 - Entity Framework

When running into this issue, please try adding parameters to your add-migration cmdlet. For example, specifying the start up project as well as the connection string name could help EF find your target database.

add-migration Delta_Defect_0973 -ConfigurationTypeName your.namespace.ContextClassName -StartUpProject DeltaProject -ConnectionStringName DeltaSQL

Where:

Delta_Defect_0973 is the name of your migration

your.namespace.ContextClassName is the name of your Configuration class in your migration folder, prefixed with the full name space.

DeltaProject is the name of your main project with your web.config or app.config file.

DeltaSQL is the name of your connection string defined in your web.config or app.config file.

Solution 9 - Entity Framework

i solved same problem like this:

  • delete old migration file
  • update-database -force
  • Add-Migration AddedEntity
  • update-database

Solution 10 - Entity Framework

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

Solution 11 - Entity Framework

Just my two cents:

My scenario:

  1. I restored my local database to a working state.
  2. Already had migrations already applied to it.
  3. Whenever I tried adding a new migration i got the error about pending migrations as mentioned my the OP.

Solution:

To get around this i just provided more explicit parameters:

Add-Migration -ConnectionString "Server=localhost\SQLEXPRESS;Database=YourDataBase;Trusted_Connection=True;" -ConnectionProviderName "System.Data.SqlClient" -verbose

I am lead to believe that you can set a setting in your app.config folder to allow you to default this behaviour so you don't have to provide explicit parameters everytime. However I am not sure on how to do this.

Solution 12 - Entity Framework

There is an ambiguity and so error. Best way is to exclude the current migration file and create new migration(add-migration) file and then copy the content of new migration to excluded file and include it again and run update-database command.

Solution 13 - Entity Framework

This isn't going to be the answer for many people, but EF will chuck this error when it can't connect to the DB. If you're working from home like I am, make sure you're still connected to your VPN!

Solution 14 - Entity Framework

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

With -Force being the important part.

Solution 15 - Entity Framework

My local database did not have the __MigrationHistory populated, or existing. I manually created the table, and then migrated the data in that table from PROD to my local database. This caused VS to think the migrations had been applied (which they had been).

Solution 16 - Entity Framework

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.

Solution 17 - Entity Framework

I had a simpler problem. VS erroneously reported this error when I had a VPN connection to a client's site connected on my workstation. The problem was that the DBMS security was set to accept requests only from my real local IP. Simply turning off the VPN resolved the problem.

Solution 18 - Entity Framework

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

Solution 19 - Entity Framework

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.

Solution 20 - Entity Framework

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.

Solution 21 - Entity Framework

I had this problem too for a database that I knew was up to date when running Add-Migration. Solved by simply running the Add-Migration command a second time. Suspect a connectivity issue, as suggested by Robin Dorbell above.

Solution 22 - Entity Framework

That was happened when i suddenly renamed class of old migration that already exist in db. I checked VCS history, determined that and renamed back. All worked afterwards.

Solution 23 - Entity Framework

I did another way. I droped database entirely and run "update-database" again in vs.

Solution 24 - Entity Framework

In my case, I forgot to add my IP address in firewall rules in Azure, basically as I was unable to connect to the database I was getting this error. So specifically for my case, I added my IP address in database firewall rules in Azure and it all worked well. Apart from this, it could be the issue of proxy/internet connection/DB username password/DB connection string etc. OR obviously, you might have pending migrations for which you need to run Update-Database command.

Solution 25 - Entity Framework

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.

Solution 26 - Entity Framework

Old post but might help someone. For me it happened because I renamed Assembly name and Default namespace of the project. So I had to update ContextKey in _MigrationHisotry table to the new value of Assembly name or Default namespace. Honestly I don't know which one should be used, because for me both are same!

Solution 27 - Entity Framework

In my case, just changed the startup project to the one that contains my migrations folder, also make sure that you selected the same project in the package console manager.

Solution 28 - Entity Framework

I suffered exactly the same problem just after reverting from a migration to another.

In my case I "targetedmigration" from "migration06" to "migration04".

I needed to delete the "migration0"6 and then I was able to force creating the "migration05". This basically means that you need to just keep the next migration after the targeted one.

Solution 29 - Entity Framework

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoman SaeedView Question on Stackoverflow
Solution 1 - Entity FrameworkLadislav MrnkaView Answer on Stackoverflow
Solution 2 - Entity FrameworkRobin DorbellView Answer on Stackoverflow
Solution 3 - Entity FrameworkgirlcodeView Answer on Stackoverflow
Solution 4 - Entity FrameworkThomasView Answer on Stackoverflow
Solution 5 - Entity FrameworkJessView Answer on Stackoverflow
Solution 6 - Entity FrameworkflexView Answer on Stackoverflow
Solution 7 - Entity Frameworknu everestView Answer on Stackoverflow
Solution 8 - Entity FrameworkYves RochonView Answer on Stackoverflow
Solution 9 - Entity FrameworkyilmazdincsoyView Answer on Stackoverflow
Solution 10 - Entity FrameworkHung VuView Answer on Stackoverflow
Solution 11 - Entity FrameworkIbrarMumtazView Answer on Stackoverflow
Solution 12 - Entity FrameworkSachin CholkarView Answer on Stackoverflow
Solution 13 - Entity FrameworkCodingCretinView Answer on Stackoverflow
Solution 14 - Entity FrameworkEvan BarkeView Answer on Stackoverflow
Solution 15 - Entity FrameworkcontactmattView Answer on Stackoverflow
Solution 16 - Entity FrameworkSimon_WeaverView Answer on Stackoverflow
Solution 17 - Entity FrameworkspadelivesView Answer on Stackoverflow
Solution 18 - Entity FrameworkSurendra MouryaView Answer on Stackoverflow
Solution 19 - Entity FrameworkTod BirdsallView Answer on Stackoverflow
Solution 20 - Entity FrameworkPritamView Answer on Stackoverflow
Solution 21 - Entity FrameworkspadelivesView Answer on Stackoverflow
Solution 22 - Entity FrameworkDoctor CoderView Answer on Stackoverflow
Solution 23 - Entity FrameworkGhadir FarzanehView Answer on Stackoverflow
Solution 24 - Entity FrameworkSiddharth SachdevaView Answer on Stackoverflow
Solution 25 - Entity FrameworkAdam MarshallView Answer on Stackoverflow
Solution 26 - Entity FrameworkRezaView Answer on Stackoverflow
Solution 27 - Entity FrameworkAmin MohamedView Answer on Stackoverflow
Solution 28 - Entity FrameworkGonzo345View Answer on Stackoverflow
Solution 29 - Entity FrameworkAGuyCalledGeraldView Answer on Stackoverflow