Skip to content
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

Fix to #33183 - Complex property causes "The multi-part identifier "xxx" could not be bound" after GroupBy + Select(x => x.First()) #33197

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,26 @@ private sealed class CloningExpressionVisitor(SqlAliasManager? sqlAliasManager,
case ColumnExpression column when _tableAliasMap.TryGetValue(column.TableAlias, out var newTableAlias):
return new ColumnExpression(column.Name, newTableAlias, column.Type, column.TypeMapping, column.IsNullable);

case StructuralTypeProjectionExpression:
var result = (StructuralTypeProjectionExpression)base.Visit(expression);

// TableMap aliases are not stored in form of expression so we need to update them manually
var tableMapChanged = false;
var newTableMap = result.TableMap.ToDictionary(x => x.Key, x => x.Value);
foreach (var (oldAlias, newAlias) in _tableAliasMap)
{
var match = newTableMap.FirstOrDefault(x => x.Value == oldAlias).Key;
if (match != null)
{
newTableMap[match] = newAlias;
tableMapChanged = true;
}
}

return tableMapChanged
? result.UpdateTableMap(newTableMap)
: result;

default:
return base.Visit(expression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ public virtual StructuralTypeProjectionExpression UpdateEntityType(IEntityType d
case RelationalAnnotationNames.TpcMappingStrategy:
case RelationalAnnotationNames.TptMappingStrategy:
newTableMap = new Dictionary<ITableBase, string>();
foreach (var (table, tableReferenceExpression) in TableMap)
foreach (var (table, tableAlias) in TableMap)
{
if (table.EntityTypeMappings.Any(m => m.TypeBase == derivedType))
{
newTableMap.Add(table, tableReferenceExpression);
newTableMap.Add(table, tableAlias);
}
}

Expand Down Expand Up @@ -331,6 +331,17 @@ public virtual StructuralTypeProjectionExpression UpdateEntityType(IEntityType d
derivedType, propertyExpressionMap, ownedNavigationMap, complexPropertyCache, newTableMap ?? TableMap, IsNullable, discriminatorExpression);
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public virtual StructuralTypeProjectionExpression UpdateTableMap(IReadOnlyDictionary<ITableBase, string> newTableMap)
=> new StructuralTypeProjectionExpression(
StructuralType, _propertyExpressionMap, _ownedNavigationMap, _complexPropertyCache, newTableMap, IsNullable, DiscriminatorExpression);

/// <summary>
/// Binds a property with this structural type projection to get the SQL representation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,13 @@ from c2 in ss.Set<Customer>()
AssertEqual(e.Complex?.Two, a.Complex?.Two);
});

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Entity_with_complex_type_with_group_by_and_first(bool async)
=> AssertQuery(
async,
ss => ss.Set<Customer>().GroupBy(x => x.Id).Select(x => x.First()));

protected DbContext CreateContext()
=> Fixture.CreateContext();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,29 @@ public override async Task Same_complex_type_projected_twice_with_pushdown_as_pa
AssertSql("");
}

public override async Task Entity_with_complex_type_with_group_by_and_first(bool async)
{
await base.Entity_with_complex_type_with_group_by_and_first(async);

AssertSql(
"""
SELECT [c3].[Id], [c3].[Name], [c3].[BillingAddress_AddressLine1], [c3].[BillingAddress_AddressLine2], [c3].[BillingAddress_Tags], [c3].[BillingAddress_ZipCode], [c3].[BillingAddress_Country_Code], [c3].[BillingAddress_Country_FullName], [c3].[ShippingAddress_AddressLine1], [c3].[ShippingAddress_AddressLine2], [c3].[ShippingAddress_Tags], [c3].[ShippingAddress_ZipCode], [c3].[ShippingAddress_Country_Code], [c3].[ShippingAddress_Country_FullName]
FROM (
SELECT [c].[Id]
FROM [Customer] AS [c]
GROUP BY [c].[Id]
) AS [c1]
LEFT JOIN (
SELECT [c2].[Id], [c2].[Name], [c2].[BillingAddress_AddressLine1], [c2].[BillingAddress_AddressLine2], [c2].[BillingAddress_Tags], [c2].[BillingAddress_ZipCode], [c2].[BillingAddress_Country_Code], [c2].[BillingAddress_Country_FullName], [c2].[ShippingAddress_AddressLine1], [c2].[ShippingAddress_AddressLine2], [c2].[ShippingAddress_Tags], [c2].[ShippingAddress_ZipCode], [c2].[ShippingAddress_Country_Code], [c2].[ShippingAddress_Country_FullName]
FROM (
SELECT [c0].[Id], [c0].[Name], [c0].[BillingAddress_AddressLine1], [c0].[BillingAddress_AddressLine2], [c0].[BillingAddress_Tags], [c0].[BillingAddress_ZipCode], [c0].[BillingAddress_Country_Code], [c0].[BillingAddress_Country_FullName], [c0].[ShippingAddress_AddressLine1], [c0].[ShippingAddress_AddressLine2], [c0].[ShippingAddress_Tags], [c0].[ShippingAddress_ZipCode], [c0].[ShippingAddress_Country_Code], [c0].[ShippingAddress_Country_FullName], ROW_NUMBER() OVER(PARTITION BY [c0].[Id] ORDER BY [c0].[Id]) AS [row]
FROM [Customer] AS [c0]
) AS [c2]
WHERE [c2].[row] <= 1
) AS [c3] ON [c1].[Id] = [c3].[Id]
""");
}

[ConditionalFact]
public virtual void Check_all_tests_overridden()
=> TestHelpers.AssertAllMethodsOverridden(GetType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,29 @@ public override async Task Same_complex_type_projected_twice_with_pushdown_as_pa
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Same_complex_type_projected_twice_with_pushdown_as_part_of_another_projection(async))).Message);

public override async Task Entity_with_complex_type_with_group_by_and_first(bool async)
{
await base.Entity_with_complex_type_with_group_by_and_first(async);

AssertSql(
"""
SELECT "c3"."Id", "c3"."Name", "c3"."BillingAddress_AddressLine1", "c3"."BillingAddress_AddressLine2", "c3"."BillingAddress_Tags", "c3"."BillingAddress_ZipCode", "c3"."BillingAddress_Country_Code", "c3"."BillingAddress_Country_FullName", "c3"."ShippingAddress_AddressLine1", "c3"."ShippingAddress_AddressLine2", "c3"."ShippingAddress_Tags", "c3"."ShippingAddress_ZipCode", "c3"."ShippingAddress_Country_Code", "c3"."ShippingAddress_Country_FullName"
FROM (
SELECT "c"."Id"
FROM "Customer" AS "c"
GROUP BY "c"."Id"
) AS "c1"
LEFT JOIN (
SELECT "c2"."Id", "c2"."Name", "c2"."BillingAddress_AddressLine1", "c2"."BillingAddress_AddressLine2", "c2"."BillingAddress_Tags", "c2"."BillingAddress_ZipCode", "c2"."BillingAddress_Country_Code", "c2"."BillingAddress_Country_FullName", "c2"."ShippingAddress_AddressLine1", "c2"."ShippingAddress_AddressLine2", "c2"."ShippingAddress_Tags", "c2"."ShippingAddress_ZipCode", "c2"."ShippingAddress_Country_Code", "c2"."ShippingAddress_Country_FullName"
FROM (
SELECT "c0"."Id", "c0"."Name", "c0"."BillingAddress_AddressLine1", "c0"."BillingAddress_AddressLine2", "c0"."BillingAddress_Tags", "c0"."BillingAddress_ZipCode", "c0"."BillingAddress_Country_Code", "c0"."BillingAddress_Country_FullName", "c0"."ShippingAddress_AddressLine1", "c0"."ShippingAddress_AddressLine2", "c0"."ShippingAddress_Tags", "c0"."ShippingAddress_ZipCode", "c0"."ShippingAddress_Country_Code", "c0"."ShippingAddress_Country_FullName", ROW_NUMBER() OVER(PARTITION BY "c0"."Id" ORDER BY "c0"."Id") AS "row"
FROM "Customer" AS "c0"
) AS "c2"
WHERE "c2"."row" <= 1
) AS "c3" ON "c1"."Id" = "c3"."Id"
""");
}

[ConditionalFact]
public virtual void Check_all_tests_overridden()
=> TestHelpers.AssertAllMethodsOverridden(GetType());
Expand Down
Loading