-
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
PendingModelChangesWarning when creating new database using .NET 9 rc.1 nightly build #34431
Comments
/cc @AndriySvyryd |
@martincostello You are calling |
So has the code just been wrong forever, but it happened to just keep working anyway before 9.0-rc.1? |
It's not wrong, that call just doesn't do anything if there are no migrations. That's why we introduced the warning. |
I'm having the same issue. Preview 7 works fine. Brand new DB, migrations apply just fine. Change nothing but upgrade to RC1 packages, start with a brand new DB and I get this warning and no migrations apply. The behavior is definitely different on RC1 packages and very unclear/unexpected with no indication as to what is actually different. I also attempted to create a new migration on top of my existing migrations, but that just creates a new empty migration and the problem remains. I think this issue should be re-opened and investigated further. |
I'm having the same issue. Preview 7 works finebut not RC1. 2024-09-20 09:38:49 System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'DbContext' 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'. Code that fails Pease re-opened |
@los93sol That sounds like a bug. Can you open a new issue and share a small runnable repro project? @kmcdonald-EQIX Did you try adding a new migration? |
Unfortunately repro is not so straightforward, I have been unable to repro outside of my real project so far so I don’t know exactly what the condition is that trips it, but sounds like at least 3 people have hit it already. I ignored the “warning” on rc1 packages and it did apply the migrations so something in that diff check seems like the culprit. I also would not expect a warning to abort like this, it results in a full on unhandled exception. Another thing I tried was dropping existing migrations and just creating a brand new one against a new DB and it failed there as well so I don’t think it’s related to any previous migrations, but specifically something in that diff method that’s out of sync with the tooling to create migrations |
After setting the code below, the error stopped occurring: |
Same experience here, ignoring the warning makes it work correctly. The fact that ignoring the warning changes the behavior and multiple people have experienced it now points at a problem in that diff method. |
I'm not sure I follow. Ignoring the warning means that the exception shouldn't be thrown anymore, it doesn't change any other behavior. To be able to investigate the underlying issue we need a repro project. |
I have the following code that runs in the Program.cs using var scope = app.Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<DbContextName>();
context.Database.Migrate(); I have one migration pending that has been created but not applied, and I'm getting the same exception. The migration is not being applied because of the exception. Ignoring the exception will allow the project to apply the pending migration correctly. options.ConfigureWarnings(warnings => { warnings.Log(RelationalEventId.PendingModelChangesWarning); }); This is not a permanent fix, but it applies the migration correctly. |
I can only reproduce this as the warning says. My model that is currently in code does not match the model that the migration is built off of. |
I have the same problem when migrating Oqtane (https://github.com/oqtane/oqtane.framework) to .NET 9 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.EventDefinition
This logic has worked in every .NET Core release from 3.1 to 8.0 - however it is throwing an exception in .NET 9. Suppressing the exception resolved the issue and allowed the migrations to be executed successfully.
This feels like a hack. |
@sbwalker Did you try creating a new migration? |
@AndriySvyryd all of the migration classes are included in the application (https://github.com/oqtane/oqtane.framework/tree/dev/Oqtane.Server/Migrations/Master) providing a seamless installation and upgrade experience (for SQL Server, SQLite, MySQL, PostgreSQL) It is not clear what you mean by "creating a new migration"? The existing migration classes were created manually without using any command line tools, etc... This is necessary because Oqtane is multi-tenant and requires specific conventions for DbContext to determine how a tenant maps to a specific database. I would certainly hope that I would not need to recreate any Migration classes... if that were true, it would mean that there was a major breaking change in EF Core. |
I see. Then indeed this is a false positive, the warning is only intended for applications using ef tools to create the migrations. You can safely |
- FIX: PendingModelChangesWarning when creating new database dotnet/efcore#34431
Same issue since updating from .net 8 to .net 9 today. |
Our team is experiencing the same issue. We followed the migration guide and still ran into this issue. There are no changes/migrations pending. Is this a bug or how do we get around this issue? |
This issue should have never been closed. I don’t mean to be negative, but a lot of us are helping vet these things early and in most cases when we point things like this out early we are ignored. I’m super disappointed with how this is being handled. |
I have SQL Server and the same issue, so don't think it is only related to PostgreSQL, Npgsql |
@los93sol and others, we're not doubting that there's an issue somewhere - either in user code, or possibly a bug in EF itself. However, we simply cannot investigate further and help out with a repro: the above contains "I have this too" comments and general text about the problem, but we're lacking a simple, clear code sample that we can look at and investigate. The fact that this issue is currently closed only means exactly that - it's not currently actionable for us, since given the lack of a repro we can't investigate. That doesn't mean that we're not here to help, and if someone does the work of putting together a repro, the issue can be reopened (or in this case, it may be better to open a new one). So I don't think there should be any reason to be disappointed - we're here to help regardless of whether this issue is currently in the closed or open state. |
Apologies, was simply trying to help other developers with a wor around: |
I got while the .net 9 is running on LTS. |
@ajcvickers I added another repro to #35181 - or were you wanting a separate issue to be created for each repro? |
@los93sol @ajcvickers I think it has to do with this line in School.cs
I updated some configs to use a constant date for the seeding of some lookup tables and the problem has went away.
I noticed even before that because of this UTC date, that it would put seeding in every migration whether it needed it or not because the date and time was different. That is at least one reason that it alwasy thinks that there is a migration needed to be applied. Not matter what you do, there will always be another migration needed because the DateTime values have changed.
|
I added a new repo with the issue. There is a pending migration to be added to the database with update-database command. The issue occurs when the update-database command is ran. Below is the url to the repo |
Remove the DateTime.UtcNow in the data seeding. that will fix it. |
I have DateTime.UtcNow in my code. Will edit to see if this fixes the issue. |
I am still getting the exception after update the DateTime.UtcNow as per Micael's suggestion. I have updated the repo with the changes.
at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition Below is my ApplicationDbContext class with the seed data |
Thanks Michael. I will try it and revert. |
@michaelakin I updated the code as suggested, the issue is still there. I have updated the repo. |
You are using |
@AndriySvyryd Changing the Id = Guid.NewGuid() to Id = Guid.Parse("6875C4A1-7668-BC3E-7FA3-F711CD6C5962") fixed the issue. This is weird anyway. I have updated the repo with a working code. |
I am still seeing this issue with the .net 9 release, am I correct in assuming that this is the intended behaviour to throw an exception when calling |
Disclaimer I am not a certified ".Net Pro" but the the Current solution that ı've found is creating seed data at run time not at migration. You can see the full solution at . I hope this solution helps to all https://github.com/mahmutozerg/DotNetNlayer/blob/main/DotNetNlayer/DotNetNlayer.API/Seeders/UserRoleSeeder.cs namespace DotNetNlayer.API.Seeders; public static class UserRoleSeeder
} |
I am not sure if Microsoft will fix it, but the problem seems to be with default dates or new GUIDS (or anything that will be different every time) in seed data or or in the models or seed data. This will cause it to think there is a new migration needed. |
If this issue is not fixed then it should at least be identified as a "breaking behavioral change" for .NET 9, as it breaks a lot of existing applications which worked fine on all prior .NET Core releases. |
I don't think that is correct, my seed data does not contain any dates or GUIDs, it just has static string, numeric and boolean values, and I am still getting the exception. I was able to bypass it by using |
@mahmutozerg Yes, for Identity it's recommended to seed at runtime and not use
@michaelakin That's a limitation of
@sbwalker Yes, we'll do it soon but note that this week is shorter due to holidays in the US. @stevenrlp Do you get an empty migration if you create a new one? If so, please file a new issue and share a small repro project. For everyone who wants to use seeding with non-deterministic values we've added a better way of doing so in 9.0 |
All Migration and Database Table Remove then An error occurred while migrating the database. |
File a bug
I have a number of applications I am testing .NET 9 nightly builds with, and after updating them yesterday to start ingesting builds of .NET 9 rc.1 I have an application where tests are failing due to an
InvalidOperationException
being thrown after a new database is created.No code changes have been made to the application, just updating to the latest .NET SDK and NuGet packages compared to the official .NET 9 preview 7 release.
The application under test creates a new database, deleting any existing database, and then performs a migration, but is now throwing an exception during migration starting that there are pending changes. There shouldn't be any pending migration as it is a newly created database.
Steps to reproduce
Clone martincostello/apple-fitness-workout-mapper@98b0602 and run
build.ps1
from the root of the repository to build the application and run the tests.Stack trace
Provider and version information
EF Core version: 9.0.0-rc.1.24402.2
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 9.0
Operating system: GitHub Actions Linux, macOS and Windows runners
IDE: None.
The text was updated successfully, but these errors were encountered: