You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the model has pending changes compared to the last migration they are not applied with the rest of the migrations when `Migrate` is called.
45
+
46
+
#### New behavior
47
+
48
+
Starting with EF Core 9.0, if the model has pending changes compared to the last migration an exception is thrown when `dotnet ef database update`, `Migrate` or `MigrateAsync` is called:
49
+
> 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'.
50
+
51
+
#### Why
52
+
53
+
Forgetting to add a new migration after making model changes is a common mistake that can be hard to diagnose in some cases. The new exception ensures that the app's model matches the database after the migrations are applied.
54
+
55
+
#### Mitigations
56
+
57
+
There are several common situations when this exception can be thrown:
58
+
59
+
- There are no migrations at all. This is common when the database is updated through other means.
60
+
- Add a migration. If you don't plan to use migrations for managin the database schema then remove the `Migrate` or `MigrateAsync` call.
61
+
- There are some migrations, but the model snapshot is missing. This is common for migrations added manually. The exception will not be thrown.
62
+
- Add a new migration, this will update the model snapshot.
63
+
- The model wasn't modified by the developer, but it's built in a non-deterministic way causing EF to detect it as modified. This is common when `new DateTime()`, `DateTime.Now`, `DateTime.UtcNow`, or `Guid.NewGuid()` are used in seed data.
64
+
- Add a new migration, examine its contents to locate the cause, and replace the dynamic data with a static, hardcoded value in the model. The migration should be recreated after the model is fixed. If dynamic data has to be used for seeding consider using [the new seeding pattern](/ef/core/what-is-new/ef-core-9.0/whatsnew#improved-data-seeding) instead of `HasData()`.
65
+
- The last migration was created for a different provider than the one used to apply the migrations.
66
+
- This is an unsupported scenario. The warning can be suppressed using the code snippet below, but this scenario will likely stop working in a future EF Core release. The recommended solution is [to generate a separate set of migrations for each provider](xref:core/managing-schemas/migrations/providers).
67
+
- The migrations are generated or choosen dynamically by replacing some of the EF services.
68
+
- The warning is a false positive in this case and should be suppressed: `options.ConfigureWarnings(w => w.Log(RelationalEventId.PendingModelChangesWarning))`
69
+
70
+
If your scenario doesn't fall under any of the above cases and adding a new migration continually creates the same migration or an empty migration and the exception is still thrown then create a small repro project and [share it with us in a new issue](https://github.com/dotnet/efcore/issues/new/choose).
0 commit comments