Skip to content
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 @@ -8,6 +8,9 @@ namespace Microsoft.EntityFrameworkCore.Query.Internal;

public partial class NavigationExpandingExpressionVisitor
{
private static readonly bool UseOldBehavior37478 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue37478", out var enabled) && enabled;

/// <summary>
/// Expands navigations in the given tree for given source.
/// Optionally also expands navigations for includes.
Expand Down Expand Up @@ -123,6 +126,16 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp

if (structuralType is not null)
{
if (!UseOldBehavior37478 && entityReference is not null && convertedType is not null)
{
structuralType = entityReference.EntityType.GetAllBaseTypes().Concat(entityReference.EntityType.GetDerivedTypesInclusive())
.FirstOrDefault(et => et.ClrType == convertedType);
if (structuralType == null)
{
return null;
}
}

var complexProperty = memberIdentity.MemberInfo != null
? structuralType.FindComplexProperty(memberIdentity.MemberInfo)
: memberIdentity.Name is not null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,18 @@ FROM root c
""");
}

public override async Task Subquery_over_primitive_collection_on_inheritance_derived_type()
{
await base.Subquery_over_primitive_collection_on_inheritance_derived_type();

AssertSql(
"""
SELECT VALUE c
FROM root c
WHERE ((c["$type"] = "SubType") AND (ARRAY_LENGTH(c["Ints"]) > 0))
""");
}

[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 @@ -256,6 +256,33 @@ private class Owned
public int Foo { get; set; }
}

[ConditionalFact] // #37478
public virtual async Task Subquery_over_primitive_collection_on_inheritance_derived_type()
{
var contextFactory = await InitializeAsync<TestContext>(
onModelCreating: mb =>
{
mb.Entity<BaseType>();
mb.Entity<SubType>();
});

await using var context = contextFactory.CreateContext();

_ = await context.Set<BaseType>()
.Where(x => ((SubType)x).Ints.Any())
.ToListAsync();
}

public abstract class BaseType
{
public int Id { get; set; }
}

public class SubType : BaseType
{
public required int[] Ints { get; set; }
}

/// <summary>
/// A utility that allows easy testing of querying out arbitrary element types from a primitive collection, provided two distinct
/// element values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,20 @@ WHERE [t].[Id] IN (@ints1, @ints2, @ints3, @ints4, @ints5, @ints6, @ints7, @ints
""");
}

public override async Task Subquery_over_primitive_collection_on_inheritance_derived_type()
{
await base.Subquery_over_primitive_collection_on_inheritance_derived_type();

AssertSql(
"""
SELECT [b].[Id], [b].[Discriminator], [b].[Ints]
FROM [BaseType] AS [b]
WHERE EXISTS (
SELECT 1
FROM OPENJSON([b].[Ints]) AS [i])
""");
}

[ConditionalFact]
public virtual async Task Same_parameter_with_different_type_mappings()
{
Expand Down
Loading