Fix complex type shadow discriminator incorrectly marked as modified on Update#38111
Closed
Fix complex type shadow discriminator incorrectly marked as modified on Update#38111
Conversation
…on Update The fix for #37337 (PR #37394) applied too broadly to all snapshot factories, storing default values for shadow properties on complex types even when reading from IInternalEntry (which can correctly access shadow values). This caused OriginalValuesFactoryFactory to store null instead of the actual discriminator value, making change detection falsely mark the discriminator as modified. Now the default-value behavior only applies when the snapshot factory parameter is not IInternalEntry (e.g., during query materialization via ShadowValuesFactoryFactory where shadow property materialization on complex types is unsupported). Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/1c384fcd-4abb-47ae-b177-e03ff1a12997 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix regression bug with init-only discriminator modification
Fix complex type shadow discriminator incorrectly marked as modified on Update
Apr 15, 2026
Member
|
Superseded by #38115 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #38105
Regression in 10.0.3:
DbSet.Update()on entities with nullable complex types having a discriminator throwsInvalidOperationException: The property 'X.Discriminator' is defined as read-only after it has been saved, but its value has been modified or marked as modified.Cause
PR #37394 (fix for #37337) changed
SnapshotFactoryFactory.CreateSnapshotExpressionto use default values for shadow properties on complex types. This was correct forShadowValuesFactoryFactory(query materialization path, where complex type shadow property materialization is unsupported per #35613), but applied too broadly to all snapshot factories — includingOriginalValuesFactoryFactory, which reads fromIInternalEntryand can resolve shadow values.The original values snapshot stored
nullfor the discriminator instead of the actual value.Update()then detected a diff against the value-generator-assigned value and marked the read-only discriminator as modified.Fix
SnapshotFactoryFactory.cs: Narrowed the default-value fallback to only apply when the factory parameter is notIInternalEntry. Entry-based factories (OriginalValues, Relationship, Sidecar) can read shadow values from the entry's shadow storage directly.AdHocComplexTypeQueryTestBase.cs: Added regression testUpdate_entity_with_nullable_complex_type_and_discriminator— seeds entity with nullable complex type + discriminator, loads it, callscontext.Update(), assertsSaveChangesAsync()succeeds.Existing #37337 test continues to pass (shadow values during query materialization still use defaults).