You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix ExecuteUpdate setter over left-joined GroupBy projection - #39721
Since #38333, when translating a LEFT JOIN onto a subquery whose projection is a non-entity NewExpression or MemberInitExpression (such as a GroupBy projection into an anonymous type or DTO), the inner shaper is wrapped in a ConditionalExpression gated on a nullability marker column:
\\csharp
marker == null ? null : new { PartId = ..., NormalHours = ... }
\
When an ExecuteUpdate setter accesses a member on that outer-joined projection (e.g. x.Agg.NormalHours.GetValueOrDefault()), RelationalSqlTranslatingExpressionVisitor.VisitMember folds the member access into both branches of the ConditionalExpression:
\\csharp
Expression.Condition(
conditional.Test,
Expression.MakeMemberAccess(conditional.IfTrue, member),
Expression.MakeMemberAccess(conditional.IfFalse, member))
\
Because conditional.IfTrue is ConstantExpression(null), Expression.MakeMemberAccess(null, member) produces ((AnonType)null).NormalHours, which fails to translate in VisitMember and causes TranslateSetters to throw InvalidOperationException: The following lambda argument to 'SetProperty' does not represent a valid value.
Additionally, if the GroupBy subquery projects into a DTO using MemberInitExpression (
ew AggDto { ... }), VisitMember previously only reduced NewExpression and not MemberInitExpression.
Description of Change
In RelationalSqlTranslatingExpressionVisitor.VisitMember:
When folding a member access into a ConditionalExpression, if either branch is a
ull constant or reference-type default, propagate a typed
ull constant (memberExpression.Type.MakeNullable()) instead of constructing a member access on
ull, and align the branch types to nullable when folding over a non-nullable value-type member.
Reduce member access over MemberInitExpression when the accessed member has a matching MemberAssignment binding, matching existing NewExpression reduction.
Added relational specification tests in NonSharedModelBulkUpdatesRelationalTestBase (along with SQL baselines in SQLite and SQL Server) covering both anonymous type (NewExpression) and DTO (MemberInitExpression) left-joined GroupBy projections in ExecuteUpdate.
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The null-propagation change incorrectly translates Nullable.HasValue to NULL instead of false on SQLite, causing ExecuteUpdate setters to write incorrect values or violate non-nullable column constraints.
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
A moderate correctness issue remains because value-type MemberInit projections are incorrectly treated as null, changing a left-joined struct DTO’s expected default value of 0 to SQL NULL without regression coverage.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Value-type DefaultExpression is incorrectly treated as null
GateNonEntityOnNullabilityMarker also produces Expression.Default(objectType) for value-type MemberInit projections, which is a zeroed struct rather than null. This condition classifies every DefaultExpression as a null target, so a left-joined mutable struct/record-struct DTO such as default(AggStruct).Count is rewritten to a nullable SQL NULL instead of the LINQ DefaultIfEmpty value 0; the new reference-type DTO test does not cover this path. Restrict the DefaultExpression case to reference types (as the existing marker checks do) and add a value-type projection regression test.
The reason will be displayed to describe this comment to others. Learn more.
GateNonEntityOnNullabilityMarker also produces Expression.Default(objectType) for value-type MemberInit projections, which is a zeroed struct rather than null. This condition classifies every DefaultExpression as a null target, so a left-joined mutable struct/record-struct DTO such as default(AggStruct).Count is rewritten to a nullable SQL NULL instead of the LINQ DefaultIfEmpty value 0; the new reference-type DTO test does not cover this path. Restrict the DefaultExpression case to reference types (as the existing marker checks do) and add a value-type projection regression test.
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
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 #39714
Root Cause
Since #38333, when translating a LEFT JOIN onto a subquery whose projection is a non-entity NewExpression or MemberInitExpression (such as a GroupBy projection into an anonymous type or DTO), the inner shaper is wrapped in a ConditionalExpression gated on a nullability marker column:
\\csharp
marker == null ? null : new { PartId = ..., NormalHours = ... }
\
When an ExecuteUpdate setter accesses a member on that outer-joined projection (e.g. x.Agg.NormalHours.GetValueOrDefault()), RelationalSqlTranslatingExpressionVisitor.VisitMember folds the member access into both branches of the ConditionalExpression:
\\csharp
Expression.Condition(
conditional.Test,
Expression.MakeMemberAccess(conditional.IfTrue, member),
Expression.MakeMemberAccess(conditional.IfFalse, member))
\
Because conditional.IfTrue is ConstantExpression(null), Expression.MakeMemberAccess(null, member) produces ((AnonType)null).NormalHours, which fails to translate in VisitMember and causes TranslateSetters to throw InvalidOperationException: The following lambda argument to 'SetProperty' does not represent a valid value.
Additionally, if the GroupBy subquery projects into a DTO using MemberInitExpression (
ew AggDto { ... }), VisitMember previously only reduced NewExpression and not MemberInitExpression.
Description of Change
ull constant or reference-type default, propagate a typed
ull constant (memberExpression.Type.MakeNullable()) instead of constructing a member access on
ull, and align the branch types to nullable when folding over a non-nullable value-type member.