Skip to content

Warn when pending model changes may be caused by an outdated migration snapshot#38065

Open
Copilot wants to merge 10 commits intomainfrom
copilot/log-warning-when-migrate-async-called
Open

Warn when pending model changes may be caused by an outdated migration snapshot#38065
Copilot wants to merge 10 commits intomainfrom
copilot/log-warning-when-migrate-async-called

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

Adds a warning (RelationalEventId.OldMigrationVersionWarning) when Migrate()/MigrateAsync() detects pending model changes and the model snapshot's ProductVersion major version is older than the running EF Core major version. In this scenario the warning fires instead of PendingModelChangesWarning, because the detected changes may be artifacts of snapshot-generation differences in the older EF Core version rather than actual model changes.

Changes

  • Migrator.ValidateMigrations() — refactored to use early-return guards for the no-migrations and no-snapshot cases; the version check is now placed inside the PendingModelChangesWarning else-branch so it only fires when pending model changes are detected and the model is deterministic
  • Trigger conditionOldMigrationVersionWarning fires instead of PendingModelChangesWarning when:
    • targetMigration is null (default Migrate() path)
    • HasPendingModelChanges() returns true
    • The model changes are deterministic (not from HasData with random values)
    • The snapshot's major version is less than the current EF Core major version
  • Major-version-only comparison — avoids noise from minor/patch/pre-release updates; implemented via a TryGetMajorVersion local function using AsSpan without substring allocation
  • MigrationVersionEventData — new event data type extending DbContextTypeEventData, carrying the DbContext type and the snapshot version string
  • RelationalEventId.OldMigrationVersionWarning — new event ID; configurable via ConfigureWarnings
// Treat as error
optionsBuilder.ConfigureWarnings(w =>
    w.Throw(RelationalEventId.OldMigrationVersionWarning));

// Suppress
optionsBuilder.ConfigureWarnings(w =>
    w.Ignore(RelationalEventId.OldMigrationVersionWarning));

Copilot AI and others added 4 commits April 8, 2026 07:59
…th old migration version

Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/0b2c029f-7de8-452c-94fd-5ea45341721c

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
… EF version

Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/0b2c029f-7de8-452c-94fd-5ea45341721c

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…n version

Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/0976f840-b9b7-490a-ab73-682a4bb05008

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…sion object

Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/973d5ba7-06b8-4b8e-b129-860f241811a5

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
@AndriySvyryd AndriySvyryd assigned roji and unassigned AndriySvyryd and Copilot Apr 8, 2026
@AndriySvyryd AndriySvyryd marked this pull request as ready for review April 8, 2026 15:49
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner April 8, 2026 15:49
Copilot AI review requested due to automatic review settings April 8, 2026 15:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new relational diagnostics warning emitted during Migrate()/MigrateAsync() when the model snapshot was produced by an older EF Core major version, to help surface scenarios where users may be missing snapshot-generation fixes after upgrading.

Changes:

  • Emit RelationalEventId.OldMigrationVersionWarning from Migrator.ValidateMigrations() based on snapshot ProductVersion major vs current EF Core major.
  • Introduce MigrationVersionEventData and corresponding logging plumbing (RelationalLoggerExtensions, resources, logging definitions, and event id).
  • Add functional/spec test wiring to validate the warning (including a SqlServer test which throws on the warning).

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs Adds sync/async tests that configure the new warning to throw and assert the resulting exception message.
test/EFCore.Relational.Specification.Tests/Migrations/MigrationsInfrastructureTestBase.cs Ensures the new warning is captured in migration test logging configuration.
src/EFCore.Relational/Properties/RelationalStrings.resx Adds localized message template for the new warning.
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs Adds the strongly-typed logging resource accessor and event definition for the new warning.
src/EFCore.Relational/Migrations/Internal/Migrator.cs Implements snapshot-version major parsing and emits the warning during migration validation.
src/EFCore.Relational/Diagnostics/RelationalLoggingDefinitions.cs Adds a logging definition slot for the new event.
src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs Adds logger extension method + DiagnosticSource event dispatch for the new warning.
src/EFCore.Relational/Diagnostics/RelationalEventId.cs Registers OldMigrationVersionWarning as a new migrations event id.
src/EFCore.Relational/Diagnostics/MigrationVersionEventData.cs Adds the DiagnosticSource payload type carrying migration TypeInfo and snapshot version string.
Files not reviewed (1)
  • src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Language not supported

…te()

Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/ac617def-dddb-4a94-bdf7-e938647d3d80

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested review from AndriySvyryd and Copilot and removed request for Copilot April 8, 2026 17:18
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 8, 2026 17:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 10 changed files in this pull request and generated 3 comments.

Files not reviewed (1)
  • src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Language not supported

}

/// <summary>
/// The last migration '{migrationName}' was created with EF Core version '{efVersion}'. The current version is newer. Consider adding a new empty migration to take advantage of any bug fixes and improvements in the current version of EF Core.
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The XML doc summary for LogOldMigrationVersion doesn't match the actual resource text in RelationalStrings.resx (it includes extra wording like "The current version is newer" / "any"). Since this file is typically generated from the .resx, please regenerate/update the designer so the docs stay in sync with the resource value.

Suggested change
/// The last migration '{migrationName}' was created with EF Core version '{efVersion}'. The current version is newer. Consider adding a new empty migration to take advantage of any bug fixes and improvements in the current version of EF Core.
/// The last migration '{migrationName}' was created with EF Core version '{efVersion}'. Consider adding a new empty migration to take advantage of bug fixes and improvements in the current version of EF Core.

Copilot uses AI. Check for mistakes.
…e targetMigration to look up TypeInfo

Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/f13e9cd1-ae37-4507-8a85-d070ea4d64bf

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
@roji
Copy link
Copy Markdown
Member

roji commented Apr 9, 2026

@AndriySvyryd does this mean all users will systematically start getting warnings the moment they upgrade to EF 11, if they have migrations generated with EF 10? If so, what are trying to achieve with that, given that 100% of EF users will get that warning (and I'm not sure what action they're expected to take)? Or have I misunderstood?

Copy link
Copy Markdown
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roji This is to avoid confusion from spurious pending model changes warning due to an old snapshot. I'll change the logic to make it clearer

…o pending-changes block, use context type

Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/cd563b80-faf4-4bbe-9139-914c9cda1361

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 9, 2026 18:16
Copilot AI changed the title Warn on old migration snapshot major version during Migrate() Warn when pending model changes may be caused by an outdated migration snapshot Apr 9, 2026
…thod

Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/7a02b983-9ee9-4dca-8d95-c4392fb2191b

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 9, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants