Entity Framework Core Applies Migration Software

I have a test and live db. To test the db-migration I use the syntax Add-Migration ...and Update-Databasein the Package Manager Console. But for live db, I want to do this programmatically when the application is running.

The following code did not help me:

context.Database.Migrate();

I have an error Invalid object name 'TempTenants'when I try to add a record to a table that does not exist. This is my new table.

But I have a table _EFMigrationsHistory. And there are all my migrations, even those that have not been applied. But I do not see the new table.

I will have the same result if I manually delete the table from test db and try to reproduce the error.

So, it context.Database.Migrate();creates only a new db with all migrations, if it does not exist, but does not update (apply migrations) the existing db.

Can I do it? And how can I resolve this?

+4
source share
1 answer

Something seems to have gone wrong. I deleted the unapplied migrations (records) from the table __EFMigrationsHistory(I also needed to return some table names, primary and foreign keys to the previous state) and restart the application.

So, context.Database.Migrate();apply migrations even for existing databases.

+1
source

Source: https://habr.com/ru/post/1664910/


All Articles