-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix materialization of required complex types with all-nullable properties via LEFT JOIN #37495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@copilot I've pushed changes to main which should allow you to execute tests against SQL Server - you skipped a test because of issues running against LocalDB, which should be gone now. Can you please rebase this PR on top of latest main and ensure that the new test passes, on SQL Server? |
|
@copilot OK, I've fixed a secrets issue that should make "initialize containers" work. Can you please try again? |
Rebased on latest main (commit 722583a) and verified the test passes on SQL Server. Both tests now pass:
The SQL Server container infrastructure is working correctly. Commit: 79d1b4c |
249ae47 to
6b86657
Compare
79d1b4c to
5e68a5c
Compare
|
@copilot I've just pushed another commit that affects running SQL Server tests; can you please repeat - rebase on latest main and ensure that the tests run correctly on SQL Server? |
5e68a5c to
23a1d98
Compare
|
@copilot I've just pushed another commit that affects running SQL Server tests; can you please repeat - rebase on latest main and ensure that the tests run correctly on SQL Server? |
Co-authored-by: roji <[email protected]>
Co-authored-by: roji <[email protected]>
Co-authored-by: roji <[email protected]>
23a1d98 to
4b0263f
Compare
|
@copilot I've just pushed another commit that affects running SQL Server tests; can you please repeat - rebase on latest main and ensure that the tests run correctly on SQL Server? |
Rebased on latest main (commit a9895a5) and verified tests pass on SQL Server. Both tests confirmed working:
Note: Tests pass when using connection string with correct password format |
Fix: Materializing non-optional complex type with all-nullable properties via LEFT JOIN returns null
Issue Summary
When a non-optional complex type (with
IsRequired(true)) has all nullable properties and is accessed via a LEFT JOIN (e.g., through a back-navigation from a derived class), EF Core 10 incorrectly materializes it as null instead of creating an instance with null properties.Root Cause
The
HandleNullableComplexTypeMaterializationmethod inStructuralTypeMaterializerSource.csapplies null-checking logic that returnsnullwhen all properties of a complex type are null. However, for required complex types (!complexProperty.IsNullable), we should always materialize an instance even when all properties are null.Fix
Modified
HandleNullableComplexTypeMaterializationto check if the complex property is required (!complexType.ComplexProperty.IsNullable) before applying the null-checking wrapper. Required complex types now bypass the null check and are always materialized, ensuring consistency with the expected EF8 behavior.Changes
Non_optional_complex_type_with_all_nullable_properties_via_left_jointest inAdHocComplexTypeQueryTestBase.csto reproduce the issue with TPH inheritance and LEFT JOIN navigationHandleNullableComplexTypeMaterializationinStructuralTypeMaterializerSource.csto check if complex property is required before applying null-checking logicTesting & Verification
Non_optional_complex_type_with_all_nullable_properties_via_left_joinpasses on SQL ServerNon_optional_complex_type_with_all_nullable_propertiescontinues to passOriginal prompt
This section details on the original issue you should resolve
<issue_title>Materializing non-optional complex type with all-nullable properties via LEFT JOIN returns null</issue_title>
<issue_description>### Bug description
Dealing with EF10, with reference to the issue #37162, I have a more complicated use case that is not behaving as expected even after applying the fix #37196.
The non-optional complex type with all properties set as nullable is declared on a derived class that is retrieved through a back-navigation:
ProjectPropertyhas a relationship toProjectwith both Forward and Back-Navigation properties.ProjectLifetimeextendsProjectPropertyand declares a complex property configured as required.In my scenario
ProjectLifetimeproperties are declared as nullable and the database values are all NULL.When I perform a query like the following, I unexpectedly get the
Lifetimeproperty of theProjectLifetimeclass unexpectedly null.The same code works fine with EF8.
I suppose that this could be a corner case while accessing complex type through a back navigation declared by a derived class.
Your code
Stack traces
Verbose output
EF Core version
10.0.0
Database provider
No response
Target framework
.NET 10.0
Operating system
No response
IDE
Visual Studio 2026 18.0.2</issue_description>
Comments on the Issue (you are @copilot in this section)
@roji Confirmed regression in 10. See below for a simpler repro - the inheritance isn't needed to reproduce this. The problem is with any access of a required complex types that has only optional properties, via a nullable navigation.Minimal repro