Why does Add-Migration sometimes create duplicate migrations?

A snapshot of the database model is saved along with every migration in a .resx file. When you add a new migration, EF compares the current database model (which is generated from your model classes and settings from your DbModelBuilder) with the last migration and determines a changes between them.

The problem you are describing can occur if your migrations are out of sync. It happens to us if two developers make two independent migrations and these migrations are later merged back to the default branch.

Example:

Developer 1

Migration AddColumnA

Developer 2

Migration AddColumnB

Merged version

Migration AddColumnA – database snapshot includes columnA

Migration AddColumnB – database snapshot includes columnB but not
columnA

If you add another migration, the changes are determined against migration AddColumnB, that doesn’t contain information about columnA. A workaround for this problem is to generate dummy migration (with empty Up and Down methods) just to have the correct database model snapshot in the last migration.

Merged version

Migration AddColumnA – database snapshot includes columnA

Migration AddColumnB – database snapshot includes columnB but not
columnA

Migration Dummy – database snapshot with columnA and columnB

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)