@@ -1334,70 +1334,8 @@ private SqlExpression BindProperty(StructuralTypeReferenceExpression typeReferen
13341334 return jsonQueryExpression . BindProperty ( property ) ;
13351335 }
13361336
1337- var projection = ( StructuralTypeProjectionExpression ) valueBufferExpression ;
1338- var propertyAccess = projection . BindProperty ( property ) ;
1339-
1340- if ( typeReference . StructuralType is not IEntityType entityType
1341- || entityType . FindDiscriminatorProperty ( ) != null
1342- || entityType . FindPrimaryKey ( ) == null
1343- || entityType . GetRootType ( ) != entityType
1344- || entityType . GetMappingStrategy ( ) == RelationalAnnotationNames . TpcMappingStrategy )
1345- {
1346- return propertyAccess ;
1347- }
1348-
1349- // Scope to actually-projected tables when known (entity-splitting).
1350- var table = entityType . GetProjectedQueryMappings ( projection . TableMap )
1351- . Single ( e => e . IsSplitEntityTypePrincipal ?? true ) . Table ;
1352- if ( ! table . IsOptional ( entityType ) )
1353- {
1354- return propertyAccess ;
1355- }
1356-
1357- // this is optional dependent sharing table
1358- var nonPrincipalSharedNonPkProperties = entityType . GetNonPrincipalSharedNonPkProperties ( table ) ;
1359- if ( nonPrincipalSharedNonPkProperties . Contains ( property ) )
1360- {
1361- // The column is not being shared with principal side so we can always use directly
1362- return propertyAccess ;
1363- }
1364-
1365- SqlExpression ? condition = null ;
1366- // Property is being shared with principal side, so we need to make it conditional access
1367- var allRequiredNonPkProperties = entityType . GetRequiredNonPkProperties ( ) ;
1368- if ( allRequiredNonPkProperties . Count > 0 )
1369- {
1370- condition = allRequiredNonPkProperties . Select ( projection . BindColumn )
1371- . Select ( c => _sqlExpressionFactory . NotEqual ( c , _sqlExpressionFactory . Constant ( null , c . Type ) ) )
1372- . Aggregate ( _sqlExpressionFactory . AndAlso ) ;
1373- }
1374-
1375- if ( nonPrincipalSharedNonPkProperties . Count != 0
1376- && nonPrincipalSharedNonPkProperties . All ( p => p . IsNullable ( ) ) )
1377- {
1378- // If all non principal shared properties are nullable then we need additional condition
1379- var atLeastOneNonNullValueInNullableColumnsCondition = nonPrincipalSharedNonPkProperties
1380- . Select ( projection . BindColumn )
1381- . Select ( c => _sqlExpressionFactory . NotEqual ( c , _sqlExpressionFactory . Constant ( null , c . Type ) ) )
1382- . Aggregate ( _sqlExpressionFactory . OrElse ) ;
1383-
1384- condition = condition == null
1385- ? atLeastOneNonNullValueInNullableColumnsCondition
1386- : _sqlExpressionFactory . AndAlso ( condition , atLeastOneNonNullValueInNullableColumnsCondition ) ;
1387- }
1388-
1389- if ( condition == null )
1390- {
1391- // if we cannot compute condition then we just return property access (and hope for the best)
1392- return propertyAccess ;
1393- }
1394-
1395- return _sqlExpressionFactory . Case (
1396- [ new CaseWhenClause ( condition , propertyAccess ) ] ,
1397- elseResult : null ) ;
1398-
1399- // We don't do above processing for subquery entity since it comes from after subquery which has been
1400- // single result so either it is regular entity or a collection which always have their own table.
1337+ return BindProperty (
1338+ ( StructuralTypeProjectionExpression ) valueBufferExpression , typeReference . StructuralType , property ) ;
14011339 }
14021340
14031341 case { Subquery : { } subquery } :
@@ -1408,7 +1346,8 @@ [new CaseWhenClause(condition, propertyAccess)],
14081346 var projectionBindingExpression = ( ProjectionBindingExpression ) shaper . ValueBufferExpression ;
14091347 var innerProjection = subSelectExpression . GetProjection ( projectionBindingExpression ) switch
14101348 {
1411- StructuralTypeProjectionExpression projection => projection . BindProperty ( property ) ,
1349+ // An included owned navigation can be an optional dependent sharing its owner's table (see TryBindMember)
1350+ StructuralTypeProjectionExpression projection => BindProperty ( projection , typeReference . StructuralType , property ) ,
14121351
14131352 // A JSON complex type bound over a subquery (see BindComplexProperty)
14141353 JsonQueryExpression jsonQuery => jsonQuery . BindProperty ( property ) ,
@@ -1426,6 +1365,70 @@ [new CaseWhenClause(condition, propertyAccess)],
14261365 }
14271366 }
14281367
1368+ private SqlExpression BindProperty ( StructuralTypeProjectionExpression projection , ITypeBase structuralType , IProperty property )
1369+ {
1370+ var propertyAccess = projection . BindProperty ( property ) ;
1371+
1372+ if ( structuralType is not IEntityType entityType
1373+ || entityType . FindDiscriminatorProperty ( ) != null
1374+ || entityType . FindPrimaryKey ( ) == null
1375+ || entityType . GetRootType ( ) != entityType
1376+ || entityType . GetMappingStrategy ( ) == RelationalAnnotationNames . TpcMappingStrategy )
1377+ {
1378+ return propertyAccess ;
1379+ }
1380+
1381+ // Scope to actually-projected tables when known (entity-splitting).
1382+ var table = entityType . GetProjectedQueryMappings ( projection . TableMap )
1383+ . Single ( e => e . IsSplitEntityTypePrincipal ?? true ) . Table ;
1384+ if ( ! table . IsOptional ( entityType ) )
1385+ {
1386+ return propertyAccess ;
1387+ }
1388+
1389+ // this is optional dependent sharing table
1390+ var nonPrincipalSharedNonPkProperties = entityType . GetNonPrincipalSharedNonPkProperties ( table ) ;
1391+ if ( nonPrincipalSharedNonPkProperties . Contains ( property ) )
1392+ {
1393+ // The column is not being shared with principal side so we can always use directly
1394+ return propertyAccess ;
1395+ }
1396+
1397+ SqlExpression ? condition = null ;
1398+ // Property is being shared with principal side, so we need to make it conditional access
1399+ var allRequiredNonPkProperties = entityType . GetRequiredNonPkProperties ( ) ;
1400+ if ( allRequiredNonPkProperties . Count > 0 )
1401+ {
1402+ condition = allRequiredNonPkProperties . Select ( projection . BindColumn )
1403+ . Select ( c => _sqlExpressionFactory . NotEqual ( c , _sqlExpressionFactory . Constant ( null , c . Type ) ) )
1404+ . Aggregate ( _sqlExpressionFactory . AndAlso ) ;
1405+ }
1406+
1407+ if ( nonPrincipalSharedNonPkProperties . Count != 0
1408+ && nonPrincipalSharedNonPkProperties . All ( p => p . IsNullable ( ) ) )
1409+ {
1410+ // If all non principal shared properties are nullable then we need additional condition
1411+ var atLeastOneNonNullValueInNullableColumnsCondition = nonPrincipalSharedNonPkProperties
1412+ . Select ( projection . BindColumn )
1413+ . Select ( c => _sqlExpressionFactory . NotEqual ( c , _sqlExpressionFactory . Constant ( null , c . Type ) ) )
1414+ . Aggregate ( _sqlExpressionFactory . OrElse ) ;
1415+
1416+ condition = condition == null
1417+ ? atLeastOneNonNullValueInNullableColumnsCondition
1418+ : _sqlExpressionFactory . AndAlso ( condition , atLeastOneNonNullValueInNullableColumnsCondition ) ;
1419+ }
1420+
1421+ if ( condition == null )
1422+ {
1423+ // if we cannot compute condition then we just return property access (and hope for the best)
1424+ return propertyAccess ;
1425+ }
1426+
1427+ return _sqlExpressionFactory . Case (
1428+ [ new CaseWhenClause ( condition , propertyAccess ) ] ,
1429+ elseResult : null ) ;
1430+ }
1431+
14291432 private Expression BindComplexProperty ( StructuralTypeReferenceExpression typeReference , IComplexProperty complexProperty )
14301433 {
14311434 switch ( typeReference )
0 commit comments