As the question indicates this applies to a migration in a development type environment that has not yet been released.
This issue can be solved in these steps:
- Restore your database to the last good migration.
- Delete the bad migration from your Entity Framework project.
- Generate a new migration and apply it to the database.
Note: Entity Framework and Entity Framework Core use slightly different command names
Step 1: Restore to a previous migration
If you haven't yet applied your migration you can skip this part. To restore your database schema to a previous point issue the Update-Database command with -TargetMigration option to specify the last good migration. For EFCore use Update-Database "Name-of-Migration"
If your entity framework code resides in a different project in your solution, you may need to use the '-Project' option or switch the default project in the package manager console.
Update-Database –TargetMigration: <name of last good migration>
For EFCore:
Update-Database <name of last good migration>
To get the name of the last good migration use the 'Get-Migrations' command to retrieve a list of the migration names that have been applied to your database, use 'Get-Migration' without the 's' if you are using EFCore.
PM> Get-Migrations
Retrieving migrations that have been applied to the target database.
201508242303096_Bad_Migration
201508211842590_The_Migration_applied_before_it
201508211440252_And_another
This list shows the most recent applied migrations first. Pick the migration that occurs in the list after the one you want to downgrade to, ie the one applied before the one you want to downgrade. Now issue an Update-Database.
Update-Database –TargetMigration: "<the migration applied before it>"
For EFCore:
Update-Database "<the migration applied before it>"
All migrations applied after the one specified will be down-graded in order starting with the latest migration applied first.
EF will reject the command if your downgrade might cause data loss. Use the '-Force' option to accept the data loss and allow the command to execute.
Step 2: Delete your migration from the project
If you are using Entity Framework Core you can use the 'remove-migration' command, for Entity Framework, delete the files of the unwanted migration in your EF project 'Migrations' folder manually. At this point, you are free to create a new migration and apply it to the database.
For EFCore:
remove-migration name_of_bad_migration
Step 3: Add your new migration
add-migration my_new_migration
Step 4: Apply your migration to the database
update-database