-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling Migrate on a database containing tables but no history throws #35181
Comments
/cc @bricelam Do you remember anything about Migrate creating the history table in this case? |
Repro:
The system will display an "Installation Wizard" screen: For simplicity you can choose SQLite for the Database option Username: host Click Install Error message will be displayed: "An Error Occurred Provisioning The Master Database. This Is Usually Related To The Master Database Not Being In A Supported State. System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'MasterDBContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'. at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition1.Log[TLoggerCategory](IDiagnosticsLogger1 logger, TParam arg) at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.PendingModelChangesWarning(IDiagnosticsLogger`1 diagnostics, Type contextType) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade) at Oqtane.Infrastructure.DatabaseManager.MigrateMaster(InstallConfig install) in C:\Source\Projects\oqtane.framework\Oqtane.Server\Infrastructure\DatabaseManager.cs:line 271" Note that the application has a full set of migrations which it runs, that are in the following folder: Oqtane.Server\Migrations Note that one unique thing that it does is create a custom __EFMigrationsHistory table: It does this because it is really helpful to know when a migration was executed and what version of the framework it was run on. Note that this installation approach has worked from .NET 3.1 to 8.0... and now throws an exception on .NET 9.0 (unless the .ConfigureWarnings is added) To rerun, simply edit the appsettings.json file and set "DefaultConnection": "" |
Create followed by CreateTables bypasses migrations to create the schema. Migrate will create the history table when it applies the first migration. If there are no migrations, I don’t think it created the history table. (But I could be wrong.) |
@PureIso That's the intended behavior. If your app does not have any migrations or the last one is out-of-date |
Any way to avoid it apart from warning suppress? |
You need to create a migration |
@sbwalker Are you using the same migrations for different providers (SQL Server, Sqlite, etc.)? |
Yes, we are using the same migrations for SQL Server, SQLite, MySQL, and PostgreSQL. It's been a real challenge... most developers naively believe that EF Core abstracts all of the complexities of supporting multiple databases... but definitely NOT when it comes to migrations (all relational databases are not created equal) |
This repro was extracted from this comment by @PureIso.
This is a case where there is a regression in behavior between EF8 and EF9, but there are likely to be other root causes for the exception. I was not able to find a case where Up/Down are empty using this repro, although a better understanding of the root cause may reveal this.
Consider code that creates tables in the
DbContext
constructor:This creates any tables that don't exist, but does not create the migrations history table:
Then, when the application starts, it calls
Database.Migrate
:In EF8, this creates the migration history table:
But in EF9, it throws:
Full code:
The text was updated successfully, but these errors were encountered: