Page 1 of 1

EF Core EnsureDelete doesn't clean up _EFMigrationsHistory Table

Posted: Tue 18 Dec 2018 14:51
by Susanna
Dear DevArt team,

I'm trying to reset the database through EnsureDelete and running the migration after.
After EnsureDelete is called all tables are deleted but the _EFMigrationsHistory table still keeps the history of the migrations and obviously doesn't run Migration again. If _EFMigrationsHistory table is manually cleaned up then migration works as expected.
Please advise.

DevArt Version 9.6.646

Thanks
Susanna

Re: EF Core EnsureDelete doesn't clean up _EFMigrationsHistory Table

Posted: Wed 19 Dec 2018 15:05
by Shalex
By Microsoft design, EnsureCreated() / EnsureDelete() do not work with __EFMigrationsHistory.

__EFMigrationsHistory is used by Code-First Migrations, so the following code will clean up the __EFMigrationsHistory table:

Code: Select all

    var migrator = context.GetService<IMigrator>();
    migrator.Migrate("0");

    // now you can call context.Database.Migrate();

Re: EF Core EnsureDelete doesn't clean up _EFMigrationsHistory Table

Posted: Thu 20 Dec 2018 10:31
by Susanna
I'm getting "table or view does not exist" on the execution of migrator.Migrate("0");

Thanks for the reply

Re: EF Core EnsureDelete doesn't clean up _EFMigrationsHistory Table

Posted: Thu 20 Dec 2018 12:52
by Shalex
Susanna wrote:
Thu 20 Dec 2018 10:31
I'm getting "table or view does not exist" on the execution of migrator.Migrate("0");
Most likely, you removed tables with EnsureDelete(), then called migrator.Migrate("0"); that looks for the tables registered in __EFMigrationsHistory and cannot find them. If you use EnsureDelete(), clear __EFMigrationsHistory manually.

Re: EF Core EnsureDelete doesn't clean up _EFMigrationsHistory Table

Posted: Thu 20 Dec 2018 15:17
by Susanna
Now I started over and not getting the exception anymore, but it seems like the code below doesn't delete the tables and I'm getting "ORA-00955: name is already used by an existing object" on next migration. Please let me know if I'm doing it wrong.
My goal is to reset the database without any manual operations.

var migrator = mContext.GetService<IMigrator>();
migrator.Migrate("0");

Thanks in advance

Re: EF Core EnsureDelete doesn't clean up _EFMigrationsHistory Table

Posted: Fri 21 Dec 2018 14:33
by Shalex
Please specify the code for reproducing the issue on an empty database, i.e. your code should create some table in the database via migration and apply migrator.Migrate("0").