Entity Framework Core apply migrations programmatically

asp.net-core ef-migrations entity-framework-core

Question

I have test and live db. For test db migrations I use Add-Migration ... and Update-Database syntax in package manager console. But for live db I want to do it programmatically when app is started.

The following code didn't help me:

context.Database.Migrate();

I have error Invalid object name 'TempTenants' when I try to add record to table that doesn't exist. This is my new table.

But I have _EFMigrationsHistory table. And there are all my migrations even those that weren't applied. But I don't see new table.

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

So, context.Database.Migrate(); only create new db with all migrations, if it doesn't exist, but don't update (apply migrations) existing db.

Can I do that? And how can I resolve that?

1
9
12/26/2016 1:23:22 PM

Accepted Answer

It seems something went wrong. I removed unapplied migrations (records) from __EFMigrationsHistory table (also I needed to revert some table names, primary and foreign keys to previous state) and launched app again.

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

1
12/27/2016 7:08:54 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow