Postegres ef-core update database produce "42P01: relation "__EFMigrationsHistory" does not exist" error

1

Setup

Asp.NET core website 2.2 and EF Core 2.2
Postgresql database with multiple schemas and one of the schema already has __EFMigrationsHistory table


when trying

Add-Migration x1 -Context YodaContext it works

but when trying the following statement
Update-Database -Context YodaContext for the first time (I do not have any tables in this schema this is the first update-database) I am seeing the following error.

Failed executing DbCommand (85ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "MigrationId", "ProductVersion"
FROM "__EFMigrationsHistory"
ORDER BY "MigrationId";
Npgsql.PostgresException (0x80004005): 42P01: relation "__EFMigrationsHistory" does not exist
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 1032
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 444
   at Npgsql.NpgsqlDataReader.NextResult() in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 332
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1218
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1130
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
42P01: relation "__EFMigrationsHistory" does not exist

What I did

  1. I searched this error and I found this bug on github the suggested solution there was to create the table manually, and I did that, but this did not solve the problem.

  2. I also tried to open a new .NET 5 project and install the latest version of the Npgsql.EntityFrameworkCore.PostgreSQL v 5.0.0 provider to connect to that database and I still facing this problem


this question does not solve the problem EF Core - Table '*.__EFMigrationsHistory' doesn't exist

postgresql
entity-framework-migrations
npgsql
ef-core-2.2
asked on Stack Overflow Nov 17, 2020 by Hakan Fıstık • edited Nov 18, 2020 by Hakan Fıstık

2 Answers

3

I also have the same issue, but I don't have permissions to delete schema and create it again in my company. I solved problem in a another way:

  1. I run command to execute migration. Add-Migration InitialIdentityUser -Context IdentityUserDbContext
  2. After that, I use tricky command Script-Migration -from 0. This command generate bunch of sql queries to create schema and tables.
  3. I use pretty cool tool - flyway for my migrations https://flywaydb.org/
  4. Execute baseline command with flyway tool.
flyway baseline -locations=filesystem:. -url=... -user=... -password=... -baselineVersion="001" -schemas=...
  1. Finally, execute migrate command to migrate all sql tables.
flyway migrate -locations=filesystem:. -url=... -user=... -password=... -schemas=...
answered on Stack Overflow Dec 20, 2020 by DarkSideMoon • edited Jan 3, 2021 by DarkSideMoon
1

I faced this problem while working with a very huge enterprise, so the solution applied by the enterprise themselves, they deleted the schema and created it again (as they told me) maybe they did some other stuff, I do not know.

After that everything went well, and the Update-Database statement worked perfectly well.

answered on Stack Overflow Nov 25, 2020 by Hakan Fıstık

User contributions licensed under CC BY-SA 3.0