I've done this kind of procedure multiple times successfully, but for the first time, it seems to not work. I want to merge two branches of my application, they have different Code First Migration states. I'm aware of the fact that during development of two versions, the schema changed in both, resulting in a different snapshots, so I need to re-scaffold my merged Migrations to match the database snapshots, basically following this guide, Option 2. In the given Project I have dozens of Migrations and multiple of them were merged and re-scaffolded successfully as described.
So what I'm doing is bringing the database state to the database version before Migration (e.g.
Update-Database -targetMigration 201511261243087_BeforeMigration, Step 4 of the Guide) and re-scaffold the merged Migrations in the correct order, so they can re-scaffold the metadata.
When I use the Command
Add-Migration 201512170931290_FirstMergedMigration, I get the following error message:
Unable to generate an explicit migration because the following explicit migrations are pending: [201512170931290_FirstMergedMigration, 201512140935307_SecondMergedMigration]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
What? I wasn't expecting this. I'm expecting the command to re-scaffold the existing migration's metadata and update the Schema snapshot. But it's assuming that I want to add a new Migration. The
-force flag doesn't help either.
When I follow the recommended step and
Update-Database, EF will create a new Migration, called:
201512210931290_201512170931290_FirstMergedMigration. Now THATs really crazy.
I wish there was a separate command to Re-Scaffold Metadata so it would maybe show a clearer error message.
Anybody seen this before?
See my own answer.
OK so it seems I found the issue here... It seems like the first migration I attempted to re-scaffold, didn't contain any schema changes, because it was only adding Indexes etc. It seems that EF will try to add a new migration if it doesn't detect the change in the given Migration. What I was able to do was to skip that Migration, continue with the next one, which resulted in the expected:
Only the Designer Code for migration '201512140935307_SecondMergedMigration' was re-scaffolded. To re-scaffold the entire migration, use the -Force parameter.
I checked the database and the
__MigrationHistory looks good, so do the Tables. It would mean less trouble if EF had generated a better error message instead of trying to add a new migration where I clearly was trying to re-scaffold.