Fix reload when nullable complex property is null#37560
Merged
AndriySvyryd merged 3 commits intodotnet:mainfrom Feb 6, 2026
Merged
Fix reload when nullable complex property is null#37560AndriySvyryd merged 3 commits intodotnet:mainfrom
AndriySvyryd merged 3 commits intodotnet:mainfrom
Conversation
dd3d33e to
d71bcfe
Compare
09884c1 to
52f6bea
Compare
52f6bea to
cde8092
Compare
AndriySvyryd
reviewed
Feb 4, 2026
AndriySvyryd
approved these changes
Feb 6, 2026
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where reloading an entity with a nullable complex property set to null in the database would throw an InvalidOperationException about required members, even when the complex property itself is optional. The root cause was that store value projections were reading non-nullable value types (e.g., int instead of int?), losing null information and causing the complex property to be incorrectly treated as non-null during reload.
Changes:
- Modified store value projection to preserve nulls for value-type members inside nullable complex properties
- Enhanced
ArrayPropertyValuesto compute null complex property flags from store values - Added comprehensive regression tests for reloading optional complex properties with null values
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/EFCore.Specification.Tests/PropertyValuesTestBase.cs | Added test for reloading optional complex property with null store values and updated seed data to include Building Two with null OptionalMilk |
| src/EFCore/Internal/EntityFinder.cs | Modified BuildProjection to convert value types to nullable when inside nullable complex properties, preserving null information from database |
| src/EFCore/ChangeTracking/Internal/ArrayPropertyValues.cs | Added new constructor overload and CreateNullComplexPropertyFlags method to compute null complex property flags from store values |
| src/EFCore/ChangeTracking/EntityEntry.cs | Updated GetDatabaseValues and GetDatabaseValuesAsync to use new constructor that computes null flags from returned value buffer |
Member
|
Thanks for your contribution! |
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.
Summary
Reloading an entity with a nullable complex property set to null in the store could throw a conceptual-null error when the complex contained required members. The store value projection was reading non-nullable value types, losing nulls and causing the complex to be treated as non-null.
Fix
ArrayPropertyValuesso reload skips nested required members when the complex is null.Closes issue when merged
Test Plan
ORCID