Skip to content

Fix reload when nullable complex property is null#37560

Merged
AndriySvyryd merged 3 commits intodotnet:mainfrom
thromel:issue-37559-reload-nullable-complex
Feb 6, 2026
Merged

Fix reload when nullable complex property is null#37560
AndriySvyryd merged 3 commits intodotnet:mainfrom
thromel:issue-37559-reload-nullable-complex

Conversation

@thromel
Copy link
Contributor

@thromel thromel commented Jan 25, 2026

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

  • Preserve nulls for value-type members inside nullable complex properties when projecting store values.
  • Compute nullable-complex null flags from store values and pass them into ArrayPropertyValues so reload skips nested required members when the complex is null.
  • Add regression coverage for reload on optional complex properties.

Closes issue when merged

Test Plan

  • Sqlite: reload optional complex property with null store values (PropertyValuesSqliteTest / Reloading_optional_complex_property_with_null_does_not_throw)
  • InMemory: reload optional complex property with null store values (PropertyValuesInMemoryTest / Reloading_optional_complex_property_with_null_does_not_throw)
  • SqlServer: reload optional complex property with null store values (PropertyValuesSqlServerTest / Reloading_optional_complex_property_with_null_does_not_throw)

ORCID

@thromel thromel requested a review from a team as a code owner January 25, 2026 02:40
@thromel thromel force-pushed the issue-37559-reload-nullable-complex branch from dd3d33e to d71bcfe Compare January 25, 2026 02:44
@thromel thromel force-pushed the issue-37559-reload-nullable-complex branch from 09884c1 to 52f6bea Compare February 3, 2026 06:01
@thromel thromel force-pushed the issue-37559-reload-nullable-complex branch from 52f6bea to cde8092 Compare February 3, 2026 20:17
Copy link

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

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 ArrayPropertyValues to 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

@AndriySvyryd AndriySvyryd merged commit fc65c2d into dotnet:main Feb 6, 2026
15 of 16 checks passed
@AndriySvyryd
Copy link
Member

Thanks for your contribution!

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.

EF 10: EntityEntry.ReloadAsync throws when nullable complex property is null

2 participants