Skip to content

Key temporal table map removals by the raw schema in SqlServerMigrationsSqlGenerator - #39099

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlserver-temporal-rename-default-schema-key
Oct 2, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/sqlserver-temporal-rename-default-schema-key

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

RewriteOperations tracks temporal information per table in a dictionary keyed by the table name and the schema exactly as written on the migration operation (which may be null). Every insert and lookup used that raw schema, but the two Remove calls in the DropTable and RenameTable branches used the schema defaulted from the model.

When a temporal table is renamed from a null schema into the model's default schema (for example after adding HasDefaultSchema("dbo") to a model with a temporal table), the entry added under the new name was removed immediately, and the following AlterTable operation on the same table threw KeyNotFoundException.

Use the raw schema for the removals so they match the keys that were inserted.

Fixes #38601

  • [x ] I've read the guidelines for contributing and seen the walkthrough
  • [ x] I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • [x ] The code builds and tests pass locally (also verified by our automated build checks)
  • [x ] Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • [x ] Tests for the changes have been added (for bug fixes / features)
  • [ x] Code follows the same patterns and style as existing code in this repo

…onsSqlGenerator

RewriteOperations tracks temporal information per table in a dictionary keyed by the table name and the schema exactly as written on the migration operation (which may be null). Every insert and lookup used that raw schema, but the two Remove calls in the DropTable and RenameTable branches used the schema defaulted from the model.

When a temporal table is renamed from a null schema into the model's default schema (for example after adding HasDefaultSchema("dbo") to a model with a temporal table), the entry added under the new name was removed immediately, and the following AlterTable operation on the same table threw KeyNotFoundException.

Use the raw schema for the removals so they match the keys that were inserted.

Fixes dotnet#38601

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

No unresolved blocking issues; the fix includes regression coverage.

Review effort: Lite
Findings: None

What changed in this PR

Fixes SQL Server temporal migration failures caused by inconsistent schema keys.

Changes:

  • Uses raw schemas for temporal map removals.
  • Adds regression coverage for schema moves.
File Summary
test/​EFCore.SqlServer.FunctionalTests/​Migrations/​SqlServerMigrationsSqlGeneratorTest.cs Adds regression coverage for issue #38601.
src/​EFCore.SqlServer/​Migrations/​SqlServerMigrationsSqlGenerator.cs Corrects temporal map removal keys.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit c718b52 into dotnet:main Oct 2, 2026
20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 3, 2026
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.

SqlServer: KeyNotFoundException in RewriteOperations when a temporal table is renamed into the model's default schema

3 participants