@@ -2275,6 +2275,9 @@ private void ApplySetOperation(
22752275 var outerIdentifiers = select1 . _identifier . Count == select2 . _identifier . Count
22762276 ? new ColumnExpression ? [ select1 . _identifier . Count ]
22772277 : [ ] ;
2278+ // A distinct set operation's result is uniquely identified by all its projected columns, so they can serve as the identifier
2279+ // even when the identifiers of the two sides can't be matched (e.g. they have different counts).
2280+ var canIdentifyByProjection = distinct && select1 . _identifier . Count > 0 && select2 . _identifier . Count > 0 ;
22782281 var entityProjectionIdentifiers = new List < ColumnExpression > ( ) ;
22792282 var entityProjectionValueComparers = new List < ValueComparer > ( ) ;
22802283 var otherExpressions = new List < ( SqlExpression Expression , ValueComparer Comparer ) > ( ) ;
@@ -2386,7 +2389,10 @@ private void ApplySetOperation(
23862389 outerIdentifiers = [ ] ;
23872390 }
23882391 }
2392+ }
23892393
2394+ if ( outerIdentifiers . Length > 0 || canIdentifyByProjection )
2395+ {
23902396 // we need comparer (that we get from type mapping) for identifiers
23912397 // it may happen that one side of the set operation comes from collection parameter
23922398 // and therefore doesn't have type mapping (yet - we infer those after the translation is complete)
@@ -2421,11 +2427,10 @@ private void ApplySetOperation(
24212427 select2 . IsMutable = false ;
24222428
24232429 // We should apply _identifiers only when it is distinct and actual select expression had identifiers.
2424- if ( distinct
2425- && outerIdentifiers . Length > 0 )
2430+ if ( canIdentifyByProjection )
24262431 {
24272432 // If we find matching identifier in outer level then we just use them.
2428- if ( outerIdentifiers . All ( e => e != null ) )
2433+ if ( outerIdentifiers . Length > 0 && outerIdentifiers . All ( e => e != null ) )
24292434 {
24302435 _identifier . AddRange ( outerIdentifiers . Zip ( select1 . _identifier , ( c , i ) => ( c ! , i . Comparer ) ) ) ;
24312436 }
@@ -2507,20 +2512,23 @@ StructuralTypeProjectionExpression ProcessStructuralType(
25072512 outerIdentifiers = [ ] ;
25082513 }
25092514 }
2510- // If the top-level projection - not the current nested one - is a complex type and not an entity type, then add
2511- // all its columns to the "otherExpressions" list (i.e. columns not part of a an entity primary key). This is
2512- // the same as with a non-structural type projection.
2513- else if ( projection1 . StructuralType is IComplexType )
2514- {
2515- var outerTypeMapping = column1 . TypeMapping ?? column1 . TypeMapping ;
2516- if ( outerTypeMapping == null )
2517- {
2518- throw new InvalidOperationException (
2519- RelationalStrings . SetOperationsRequireAtLeastOneSideWithValidTypeMapping ( setOperationType ) ) ;
2520- }
2515+ }
25212516
2522- otherExpressions . Add ( ( outerColumn , outerTypeMapping . KeyComparer ) ) ;
2517+ // If the top-level projection - not the current nested one - is a complex type and not an entity type, then add
2518+ // all its columns to the "otherExpressions" list (i.e. columns not part of a an entity primary key). This is
2519+ // the same as with a non-structural type projection. Identifier columns are included too, since this list is
2520+ // only used when the outer identifiers couldn't all be matched.
2521+ if ( ( outerIdentifiers . Length > 0 || canIdentifyByProjection )
2522+ && projection1 . StructuralType is IComplexType )
2523+ {
2524+ var outerTypeMapping = column1 . TypeMapping ?? column2 . TypeMapping ;
2525+ if ( outerTypeMapping == null )
2526+ {
2527+ throw new InvalidOperationException (
2528+ RelationalStrings . SetOperationsRequireAtLeastOneSideWithValidTypeMapping ( setOperationType ) ) ;
25232529 }
2530+
2531+ otherExpressions . Add ( ( outerColumn , outerTypeMapping . KeyComparer ) ) ;
25242532 }
25252533 }
25262534
@@ -2633,7 +2641,7 @@ JsonQueryExpression ProcessJson(JsonQueryExpression jsonQuery1, JsonQueryExpress
26332641 type , propertyExpressions , complexPropertyCache , nullable : false , discriminatorExpression ,
26342642 structuralProjection1 . TableMap ) ;
26352643
2636- if ( outerIdentifiers . Length > 0 && outerProjection is { StructuralType : IEntityType entityType } )
2644+ if ( canIdentifyByProjection && outerProjection is { StructuralType : IEntityType entityType } )
26372645 {
26382646 var primaryKey = entityType . FindPrimaryKey ( ) ;
26392647
0 commit comments