Consolidating EF migrations into new InitialCreate

后端 未结 4 1712
轮回少年
轮回少年 2021-02-01 01:18

I have been using EF migrations for some time now and have more than 100 migration files in my project. I would like to consolidate these into a single migration before moving f

4条回答
  •  名媛妹妹
    2021-02-01 01:56

    Below procedure has the benefit of working without doing anything with the DBs, __MigrationHistory can stay as-is. Also it will work if you have multiple different environments with different versions of the structure - provided you have the branches to match.

    I turn the last migration into an initial migration. The trick is to use the oldest version of the code and DB that is in use, replace its last migration with a new initial migration and delete all previous migrations. Newer branches keep the more recent migrations so those will still work after merging to older branches.

    So start in the OLDEST branch - PROD, normally - and do:

    1. Remove all but the last migration
    2. Remove the migration code in both the "Up" and "Down" methods in the last migration
    3. Change build action of the last migration to "None" to let EF ignore it
    4. Change active connection to point to a local DB database.
    5. Make sure this local DB database does not exist
    6. add-migration Initial
    7. Copy Up and Down code from the created "Initial" migration to the last migration
    8. Delete Initial migration
    9. Change build action of the last migration back to "Compile"
    10. Check in
    11. Merge changes up
    12. Test in DEV branch on LocalDB DB - it should do the new initial migration as well as the subsequent ones with no issues
    13. Test in main branch on the latest DB - it shouldn't do anything

    Note above only works if you don't add stuff to the migrations that EF doesn't do itself. E.g. if you add DB views etc. than the newly created migration won't get those, it only gets the scripts EF generates based on your code.

提交回复
热议问题