Search code examples
c#asp.net-coreef-code-firstentity-framework-coreaspnetboilerplate

How to solve Update-Database issue when switching branch?


I have one entity which has a column as int in Branch A. Now I switch to a different Branch B. There I create a migration and change column to varchar and run Update-Database. Now If I switch to Branch A again. If I see in the code the column datatype is int, but in the database, its datatype is varchar. But I want its datatype to be int. I can not even remove this migration from Branch A because it was created in Branch B.I can see only one way to solve this issue is to delete the database and run Update-Database, But I will lose all data by doing this. Is there any better way to solve this issue.

I'm using entity framework core 2.0.1.


Solution

  • I can think of two options:

    1. Use a different database for each branch (probably the best idea, IMHO)
    2. Before switching back to branch A, un-apply the migration from the database with Update-Database name-of-previous-migration

    For example, let's assume you have the following migrations:

    • 20180214094058_Initial.cs
    • 20180216100541_FirstMigration.cs
    • 20180218185111_MigrationBranchB.cs

    You can un-apply MigrationBranchB with Update-Database FirstMigration (this will call the Down method of MigrationBranchB).

    Relevant documentation:

    Reverting a migration

    If you already applied a migration (or several migrations) to the database but need to revert it, you can use the same command to apply migrations, but specify the name of the migration you want to roll back to.

    Update-Database LastGoodMigration