I have inherited an application that uses Entity Framework Core, along with a dump of its database.
Unfortunately the application did not use EF migrations.
To get it on track I've created a first migration. But trying to update the database, I get an error because the tables already exists, which is completely understandable.
As the model in the code should match the relational model, nothing should be done, only initializing the __EFMigrationsHistory
table and similar stuff.
Is there a way to bootstrap EF Core migrations in such a scenario, by only applying the initial diff (hopefully nothing if the code is consistent with the schema)?
Otherwise I guess I'll have to create a new empty database, let EF Core bootstrap its schema, then copy the data from the original database.
I'm using the latest EF Core 9.0.2 if it matters, as things evolve on a regular basis.
One workaround I can think of is to manually create the __EFMigrationsHistory
table in your database, and inserting a record to signify that your database is "up-to-date" with the snapshot that EF creates.
The __EFMigrationsHistory
table has only 2 simple fields in it:
Side Note: I'm using
Postgres
as my database engine, so the data types of the 2 fields above will be different if you're using something else.
Once you insert a record to indicate that your "initial migration" is already applied, you should be OK to make new migrations afterwards and run them on your existing database.
An example of what I inserted in my database is the following:
insert into "__EFMigrations" (MigrationId, ProductVersion) values ('20240619145227_v0001', '8.0.6')