question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conflict with __EFMigrationsHistory table in schema other than public

See original GitHub issue

Given two DbContexts each with their own default schema, Let’s say ContextOne with the default schema of public and ContextTwo with an alternate schema of, say, example, I cannot use both DBContexts in the same database without overriding the default table name of the migrations history table.

The culprit appears to be this SQL that searches for the migration history table:

SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE c.relname='__EFMigrationsHistory');

This will return a result if the __EFMigrationsHistory table exists in any schema. So, if, say, ContextTwo runs its migrations first, creates its __EFMigrationsHistory table in its schema of example, then ContextOne runs its migrations, the above SQL falsely reports that its __EFMigrationsHistory table exists. The migrations then fail because the inserts into the migration history table (assumed to be on the default public schema) attempt to insert into a table that doesn’t exist.

The workaround that I’ve used is to override the migration history table’s name such that it’s unique across all schemas.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
floyd-maycommented, Jul 16, 2019

We’re likely to skip 2.2 and go straight to 3.0 given the dependency rats nest that is ASP.Net Core 2.2 that they’ve hopefully fixed in 3.0. Thanks for the hard work y’all put into this project!!!

2reactions
rojicommented, Jan 15, 2019

@floyd-may I understand but could not reproduce the issue: I successfully created two contexts with the same migration history table name - but different schemas - and I can see both in the database. The query I’m seeing is:

SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='foo' AND c.relname='__CrapMigrations')

Can you please test on v2.2 to see if the problem goes away, and if not, post exact, detailed step-by-step instructions on how to reproduce the issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

change schema of "__EFMigrationsHistory" table
Is it possible to change schema of __EFMigrationsHistory table in EntityFramework Core? entity-framework-core · Share.
Read more >
Part 4, Razor Pages with EF Core migrations in ASP.NET ...
Migrations updates the database schema to match changes in the data ... The __EFMigrationsHistory table keeps track of which migrations have ...
Read more >
Migrate SQL Server Database to PostgreSQL using EF ...
Before starting to migrate the schema, we need to consider some differences between SQL Server and Postgres: 1- Table and field naming:.
Read more >
dotConnect for PostgreSQL History
The bug with detecting FK relationships to tables in other schemas by Create Model Wizard and Update From Database Wizard is fixed.
Read more >
Entity Framework Core Migrations
EF Core migrations enable you to track and manage these changes to the schema over time.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found