markau
markau

Reputation: 922

Subsequent EF migrations fail on database-update: Table already exists

I am able to successfully create a migration with Add-Migration InitialMigration, and apply it with update-database, per the Microsoft doco.

Making changes to my model, I add a second migration Add-Migration UpdatedModel, and this works. However, it is not a cumulative change - it is just like the initial migration, with the Up method containing CreateTable statements.

As a result, the update-database command fails on that second and subsequent attempts, complaining that the tables already exist.

Am I expecting too much from EF or does this seem like an issue? It seems that the point of the migrations is to manage cumulative updates.

The obvious workaround is to drop the database, and/or delete all migration files, so that subsequent migrations are always the initial one. This will get messy as I start to get some data beyond seed data into the test database.

Upvotes: 2

Views: 1726

Answers (1)

markau
markau

Reputation: 922

The problem was that the Migrations/AssetContextModelSnapshot.cs file got generated with the initial migration, however, it was not included in the project. Therefore, the project did not have the history of migrations available to it, and each migration was new. Right-click -> Include in project + save all, and problem solved!

Upvotes: 2

Related Questions