Skip to content

Commit f7b0f83

Browse files
authored
Add breaking change documentation for pending model changes (#4895)
Fixes #4891
1 parent 038b993 commit f7b0f83

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

entity-framework/core/what-is-new/ef-core-9.0/breaking-changes.md

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,53 @@ EF Core 9 targets .NET 8. This means that existing applications that target .NET
2323
> [!NOTE]
2424
> If you are using Azure Cosmos DB, please see the [separate section below on Azure Cosmos DB breaking changes](#azure-cosmos-db-breaking-changes).
2525
26-
| **Breaking change** | **Impact** |
27-
|:-----------------------------------------------------------------------------------------------------|------------|
28-
| [`EF.Functions.Unhex()` now returns `byte[]?`](#unhex) | Low |
29-
| [SqlFunctionExpression's nullability arguments' arity validated](#sqlfunctionexpression-nullability) | Low |
30-
| [`ToString()` method now returns empty string for `null` instances](#nullable-tostring) | Low |
31-
| [Shared framework dependencies were updated to 9.0.x](#shared-framework-dependencies) | Low |
26+
| **Breaking change** | **Impact** |
27+
|:----------------------------------------------------------------------------------------------------------|------------|
28+
| [Exception is thrown when applying migrations if there are pending model changes](#pending-model-changes) | High |
29+
| [`EF.Functions.Unhex()` now returns `byte[]?`](#unhex) | Low |
30+
| [SqlFunctionExpression's nullability arguments' arity validated](#sqlfunctionexpression-nullability) | Low |
31+
| [`ToString()` method now returns empty string for `null` instances](#nullable-tostring) | Low |
32+
| [Shared framework dependencies were updated to 9.0.x](#shared-framework-dependencies) | Low |
33+
34+
## High-impact changes
35+
36+
<a name="pending-model-changes"></a>
37+
38+
### Exception is thrown when applying migrations if there are pending model changes
39+
40+
[Tracking Issue #33732](https://github.com/dotnet/efcore/issues/33732)
41+
42+
#### Old behavior
43+
44+
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+
- **Mitigation**: If you don't plan to use migrations for managing the database schema then remove the `Migrate` or `MigrateAsync` call, otherwise add a migration.
61+
- There is at least one migration, but the model snapshot is missing. This is common for migrations created manually.
62+
- **Mitigation**: Add a new migration using EF tooling, 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 objects supplied to `HasData()`.
64+
- **Mitigation**: 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+
- **Mitigation**: 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+
- **Mitigation**: The warning is a false positive in this case and should be suppressed:
69+
70+
`options.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning))`
71+
72+
If your scenario doesn't fall under any of the above cases and adding a new migration creates the same migration each time or an empty migration and the exception is still thrown then create a small repro project and [share it with the EF team in a new issue](https://github.com/dotnet/efcore/issues/new/choose).
3273

3374
## Low-impact changes
3475

0 commit comments

Comments
 (0)