Martin Staufcik
Martin Staufcik

Reputation: 9490

EF migrations ignore data changes

I have an initial DB migration. Now I need to create the first incremental migration. When I create the first incremental migration with

add-migration Incremental1

it contains not only the table changes but all data changes as well. For example for rows that were added in between into the table it contains DeleteData commands:

migrationBuilder.DeleteData(
            table: "SystemResources",
            keyColumn: "Id",
            keyValue: 1);

Is there some setting for generating only table changes (added/removed columns) and ignoring all data changes (inserted data rows)?

Upvotes: 1

Views: 2273

Answers (1)

Martin Staufcik
Martin Staufcik

Reputation: 9490

The solution is to:

  1. disable seeding of data by commenting out all modelBuilder.Entity<T>().HasData statements,
  2. create the incremental migration with add-migration,
  3. manually remove all DeleteData / InsertData commands from the incremental migration,
  4. run update-database.

After this, the seed data is no longer part of the model snapshopt (<Database>ModelSnapshot.cs file) and is therefore no longer tracked by migrations.

This also means the next incremental migration will not contain any DeleteData / InsertData statements.

Upvotes: 3

Related Questions