Manually Editing an EF7 Migration Class & Snapshot

code-first-migrations entity-framework-core

Question

The EF7 migration add command (to date, beta5) compares the model classes defined in the DbContext against the current model snapshot, creates a new migration class, and updates the model snapshot.

I need to modify the migration to make it generate different DDL SQL. As an example, EF7 uses sequences for SQL Server auto-increment values, and I would like it to use identity. However it could be any other reason. The migration remove command would physically delete the migration files and revert the model snapshot, so it's useless in this case.

There are 3 files that contain related code that look like they need to be edited:

  1. The primary migration class: The Up and Down methods should be modified.
  2. The DbContextModelSnapshot file contains Annotations that need to be modified
  3. The secondary migration partial class: The badly named [migration].Designer.cs file also contains a model snapshot for the migration. I'm assuming this snapshot needs to match the model snapshot in item 2, but am not certain. The only information I have about the purpose of it is from Brice's blog , which says "It’s there in case you or a provider need to inspect the model for additional information during a migration."

Specific questions:

  1. Do the two model snapshots need to be kept in sync in order to correctly perform migrations?
  2. Is modifying 3 separate files the only way to edit the migration? (Although depending on the changes, the model snapshots may not have to be touched in some cases.)
  3. Is there some EF command that would regenerate just the model snapshots, but not the migration methods?

Accepted Answer

Specific answers:

  1. Do the two model snapshots need to be kept in sync in order to correctly perform migrations?
    No, the snapshot in the migration is a last resort for provider writers. For example, SQLite can't rename columns so it could use migration's model snapshot to do a table rebuild for this operation. 99% of the time, it won't ever be used.

  2. Is modifying 3 separate files the only way to edit the migration?
    Most of the time, you should only ever edit the main migration file. In rare cases, you may need to edit the model snapshot if you're working in a team environment and you encounter a merge conflict. You can ignore the designer file; it just captures some metadata about the migration.

    You may not have to edit anything if you configure your model correctly. For example, to use identity instead of sequences, override DbContext.OnModelCreating() and add modelBuilder.ForSqlServer().UseIdentity().

  3. Is there some EF command that would regenerate just the model snapshots, but not the migration methods?
    No, it shouldn't be needed since you almost never edit these files.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why