Skip to content

[release/11.0] Preserve generated values when replacing entries - #39629

Merged
AndriySvyryd merged 3 commits into
release/11.0from
andriysvyryd-triage-onadd-replacement
Nov 16, 2026
Merged

AndriySvyryd merged 3 commits into
release/11.0from
andriysvyryd-triage-onadd-replacement

Conversation

@AndriySvyryd

@AndriySvyryd AndriySvyryd commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Fixes #39617

Description
When a deleted entity and its replacement share a row, EF can mistake the deleted entity's null placeholder for a value to propagate to the replacement. Track whether the current value came from a deletion, while preserving the precedence of a modified shared-column writer.

Customer impact
Replacing an owned instance or deleting and re-adding an entity with the same key can cause SaveChanges to throw. Nullable generated-on-add values can instead silently become null in the tracked entity while the database retains the original value; subsequent operations may act on incorrect in-memory data. Configuring affected properties with ValueGeneratedNever() is a workaround, but changes insert behavior and is not obvious.

How found
User reported on EF Core 11.0.0.

Regression
Yes, from 10.0.*, Introduced by #37751.

Testing
Three new parameterized relational tests (12 cases per provider).

Risk
Low, the change affects shared-column value propagation across relational providers, but a quirk mode was added.

AndriySvyryd and others added 2 commits November 12, 2026 22:34
- Preserve values from added entries after a shared row deletion\n- Cover owned replacements, re-added entities, and shared writers\n\nFixes #39617\n\nCo-authored-by: Copilot App <223556219+Copawat@users.noreply.github.com>
- Explain why deleted entries are not current writers

Fixes #39617

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-code-quality

github-code-quality Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage

release/11.0 40c6a06 andriysvyryd-triage-... 0b5b3ea +/-
76% 67% -9%
View coverage of the 10 most impacted files.
File release/11.0 40c6a06 andriysvyryd-triage-... 0b5b3ea +/-
/Users/runner/w...nal/Snapshot.cs 0% 60% +60%
D:\a\efcore\efc...nal\Snapshot.cs 0% 64% +64%
/home/runner/wo...nal/Snapshot.cs 0% 66% +66%
/home/runner/wo...yTypeBuilder.cs 0% 88% +88%
D:\a\efcore\efc...yTypeBuilder.cs 0% 88% +88%
/Users/runner/w...ngs.Designer.cs 0% 92% +92%
/home/runner/wo...ctExpression.cs 0% 93% +93%
D:\a\efcore\efc...ctExpression.cs 0% 93% +93%
D:\a\efcore\efc...ngs.Designer.cs 0% 94% +94%
/home/runner/wo...ngs.Designer.cs 0% 95% +95%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 13, 2026 14:53 UTC

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review November 13, 2026 13:01
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner November 13, 2026 13:01
Copilot AI balanced review requested due to automatic review settings November 13, 2026 13:01

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.

🟢 Approval recommended

No active review issues were identified for the fix preventing deleted-entry placeholders from overwriting generated-on-add values.

No open issues 🎉

What changed in this PR

This servicing fix addresses #39617 in EF Core’s relational update pipeline, preventing deleted entries’ null placeholders from overwriting generated-on-add values when an entity is replaced.

Changes:

  • Tracks whether a shared-column value came from a deleted entry while preserving modified-writer precedence.
  • Adds an AppContext switch to restore the previous behavior.
  • Adds three parameterized regression tests covering owned-instance replacement, same-key re-addition, and modified shared-column values.
File Description
test/​EFCore.Relational.Specification.Tests/​Update/​NonSharedModelUpdatesTestBase.cs Adds synchronous and asynchronous checks of tracked and persisted replacement values.
src/​EFCore.Relational/​Update/​ModificationCommand.cs Distinguishes deletion placeholders from current writer values and adds the compatibility switch.

Review effort: Balanced

@artl93 artl93 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.

regression. customer reported. approved.

@AndriySvyryd
AndriySvyryd merged commit b718fc7 into release/11.0 Nov 16, 2026
35 of 36 checks passed
@AndriySvyryd
AndriySvyryd deleted the andriysvyryd-triage-onadd-replacement branch November 16, 2026 21:49
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