When both update-database and add-migrations fail while referring to each other

Today I checked out a project from Git on a new machine. The project was using Entity Framework to connect to a local SQL Server on my developer machine.

To install the database I ran update-database in the Package Manager Console and got an error message complaining about “pending changes” and that I should run add-migration to fix that. Clearly changes had been made to the model that hadn’t been included in the migrations yet.

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the 
target database.
Applying explicit migrations: [201304231444119_initial, 201304250720057_step1, 
201304250813140_step2, 201304250903413_step3].
Applying explicit migration: 201304231444119_initial.
Applying explicit migration: 201304250720057_step1.
Applying explicit migration: 201304250813140_step2.
Applying explicit migration: 201304250903413_step3.
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. You can use the Add-Migration command to write the 
pending model changes to a code-based migration.
PM>

However, as seen below, when trying to run add-migration with a new step (I didn’t want to change the existing steps since they correspond to completed steps for other developers in the team), Entity Framework complained that I had pending migrations that I hadn’t run update-database on yet.

PM> add-migration step4
Unable to generate an explicit migration because the following explicit 
migrations are pending: [201304231444119_initial, 201304250720057_step1, 
201304250813140_step2, 201304250903413_step3]. Apply the pending explicit 
migrations before attempting to generate a new explicit migration.
PM>

A little moment 22 where both commands fails while referring to the other one.

The solution was to update the database with a specific target migration specified. When running the update in the following way it all worked well.

PM> update-database -Target:201304250903413_step3
Specify the '-Verbose' flag to view the SQL statements being applied to the 
target database.
Applying explicit migrations: [201304231444119_initial, 201304250720057_step1, 
201304250813140_step2, 201304250903413_step3].
Applying explicit migration: 201304231444119_initial.
Applying explicit migration: 201304250720057_step1.
Applying explicit migration: 201304250813140_step2.
Applying explicit migration: 201304250903413_step3.
PM>

I could after that go ahead and add the missing migration as a step 4.

PM> add-migration step4
Scaffolding migration 'step4'.
The Designer Code for this migration file includes a snapshot of your current 
Code First model. This snapshot is used to calculate the changes to your model 
when you scaffold the next migration. If you make additional changes to your 
model that you want to include in this migration, then you can re-scaffold it 
by running 'Add-Migration step4' again.
PM>