Update model snapshot of last migration in Entity Framework and reapplying it

10,374

Solution 1

I'm doing something similar. I have a large database and I am using the EF Tools for VS 2013 to reverse engineer it in small parts into my DEV environment. The tool creates my POCOs and Context changes in a separate folder. I move them to my data project, create a fluent configuration and then apply a migration (or turn automigration on).

After a while I want a single migration for TEST or PROD so I roll them up into a single migration using the technique explained here: http://cpratt.co/migrating-production-database-with-entity-framework-code-first/#at_pco=smlwn-1.0&at_si=54ad5c7b61c48943&at_ab=per-12&at_pos=0&at_tot=1

Solution 2

You can simplify the steps for updating DbContext snapshot of the last migration applied to database by re-scaffolding it with Entity Framework:

  1. Revert the last migration if it is applied to the database: Update-Database -Target:Previous_Migraton
  2. Re-scaffold the last migration Add-Migration The_name_of_the_last_migration which will recreate the last migrations *.resx and *.Designer.cs (not the migration code), which is quite handy.

Those 2 steps are covering 4 steps (2-5) from original question.

You can also get different bahavior depending on what you want by specifying the flags -IgnoreChanges and (or) -Force

And by the way, the major problem with the updating the DbContext snapshot is not how to automate those steps, but how to conditionally apply them to TEST/PROD environments depending on whether you actually want to suppress the warning because you've mapped existing DB-first entities in you DbContext or you want it it to fail the build in case you've created new entities and forgot to create a code-first migration for them.

So, try to avoid those steps altogether and maybe create empty migrations when you just want to map existing tables to your code.

Share:
10,374
2ooom
Author by

2ooom

Updated on June 04, 2022

Comments

  • 2ooom
    2ooom almost 2 years

    I'm using EF6 code-first migrations for existing database but initial DbContext does not fully cover existing schema (since it's massive). So from time to time I have to make updates to the model in database-first style. For example when I need an entity mapping for a table or a column that is already in the database but not reflected in the code I do the following:

    1. Make all change (add new entity, rename the column mapping or add new property)
    2. Scaffold migration representing the latest model snapshot stub_migration
    3. Copy-paste latest serialized model from stub_migration to the last_migration resource file
    4. Delete stub_migration
    5. Revert last_migration in database
    6. Update-Database so that model snapshot in [__MigrationHistory] table would be also updated

    I understand that this aproach is a bit hackish and the proper way would be to leave empty stub_migration but this would force lots of empty migrations which I would rather avoid.

    Looking at a similar scenario from MSDN article (Option 2: Update the model snapshot in the last migration) I wouldn't imagine that there is an easier way rather than writing power shell script, managed code or both to make it work. But I would rather ask community first before diving deep into it. So I wonder: is there a simple way to automate generation of new model snapshot in latest migration and reaplying it?