Failed to create explicit migration (EF5) (pending migrations)

We use the first code conversions with EF5 on (localdb) \ v11.0 (Vstudio 2012), and everything went well.

However - today I needed to create a couple of indexes on several tables and run into problems.

First I did it in PM:

PM> add-migration AddIdxToOutage Scaffolding migration 'AddIdxToOutage'. 

I modified the code in the forest migration:

 public override void Up() { Sql(@"CREATE NONCLUSTERED INDEX [idx_WtgId_StartDateTime_EndDateTime] ON [dbo].[Outages] ( [WtgId] ASC, [StartDateTime] ASC, [EndDateTime] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]"); } 

I updated the database and the result was as follows:

 PM> update-database -startupprojectname D3A.Data -force -verbose Using StartUp project 'D3A.Data'. Using NuGet project 'D3A.Data'. Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Target database is: 'D3A.Data.StorageContext' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Convention). Applying code-based migrations: [201310301258520_AddIdxToOutage]. Applying code-based migration: 201310301258520_AddIdxToOutage. CREATE NONCLUSTERED INDEX [idx_WtgId_StartDateTime_EndDateTime] ON [dbo].[Outages] ( [WtgId] ASC, [StartDateTime] ASC, [EndDateTime] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] [Inserting migration history record] Running Seed method. 

X was created on the table, and everyone was happy.

However - when I created the following index, I ran into problems. I created an empty migration task as

 PM> add-migration AddIdxToState Unable to generate an explicit migration because the following explicit migrations are pending: [201310301258520_AddIdxToOutage]. Apply the pending explicit migrations before attempting to generate a new explicit migration. 

Excuse my French - but WT *?

It seems that this is not so. I can go back to the migration step before adding the first idx, add idx'es again and repeat the same thing.

Can you tell me what I am missing here? What am I doing wrong?

Edit:

Initially, I thought that my problem was that I was executing raw SQL against the / localdb database, but it seems like everything I do now stops after the first add-migration.

I just added a new table to the database, and this is the std-out result from the PM Console:

 PM> add-migration AddMyLoggerTable Scaffolding migration 'AddMyLoggerTable'. 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 201310301419063_AddMyLoggerTable' again. PM> update-database -startupproject DongEnergy.D3A.Data -verbose Using StartUp project 'D3A.Data'. Using NuGet project 'D3A.Data'. Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Target database is: 'D3A.Data.StorageContext' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Convention). Applying code-based migrations: [201310301419063_AddMyLoggerTable]. Applying code-based migration: 201310301419063_AddMyLoggerTable. CREATE TABLE [dbo].[MyLoggerTable] ( [id] [int] NOT NULL, [functionId] [int], [elapsedTime] [bigint], [noOfRecords] [int], [dateCreated] [datetime] DEFAULT getDate() ) [Inserting migration history record] Running Seed method. PM> add-migration AddMyLoggerTableFunction Unable to generate an explicit migration because the following explicit migrations are pending: [201310301419063_AddMyLoggerTable]. Apply the pending explicit migrations before attempting to generate a new explicit migration. 

Notice how I add a new, empty migration task, use the CreateTable method and successfully updated it in the database. But when I add a new transition step, he complains that a task that was simply β€œfixed” in the database is still β€œwaiting” - although both migrationhistory objects and databases are updated.

+8
indexing entity-framework code-first-migrations localdb
source share
3 answers

This may not solve the OP question, but I had a similar problem when I was developing a new database and trying to add two migrations before doing any database update.

My solution was to run my application so that pending migrations could update the database. The application automatically updates the database using MigrateDatabaseToLatestVersion . This can be done by typing Update-Database from the package manager console.

If you added a migration and made additional changes that need to be added to the same migration, there is also no need to delete it and re-add it (which I tried to do first). You can simply upgrade an existing pending migration. Help is displayed when I start the Add-Migration tool from the package manager console (highlighted by me).

The constructor code for this migration file contains a snapshot of your current Code First Model. This snapshot is used to calculate the changes in your model when you pick up the next migration. If you make additional changes to your model that you want to include in this migration, then you can raise it by running 'Add-Migration [Timestamp] _MigrationName' again .

In other words, run the Add-Migration tool with the same identifier as the pending operation, and the new changes will be merged with the old one. If you notice that the changes are not merged, you can use the -F flag (as in Add-Migration <Name> -Force ) to force the migration, like the anthony-arnold notes in the comments.

+8
source share

Another possible reason is if there are different projects in the solution, such as UI and Core (your db configuration is here), then make sure that during the migration your main project is set as the Startup project. This solved my problem. Happy coding :)

+4
source share

Just in this case, I'm interested. I encountered the error "Failed to create an explicit migration because the next explicit migration is expected ..." after I changed the ContextKey value in the Configuration.cs file. I changed it and the problem disappeared.

+2
source share

All Articles