Skip to content

Commit

Permalink
Fix query filters with context accessors (dotnet#35237)
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet committed Dec 2, 2024
1 parent 1c0ef32 commit c7e1061
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,11 @@ protected override Expression VisitConditional(ConditionalExpression conditional
goto case StateType.ContainsEvaluatable;

case StateType.ContainsEvaluatable:
// The case where the test is evaluatable has been handled above
if (testState.IsEvaluatable)
{
test = ProcessEvaluatableRoot(test, ref testState);
}

if (ifTrueState.IsEvaluatable)
{
ifTrue = ProcessEvaluatableRoot(ifTrue, ref ifTrueState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,4 +730,42 @@ public class ChildFilter2
}

#endregion

#region 35111

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Query_filter_with_context_accessor_with_constant(bool async)
{
var contextFactory = await InitializeAsync<Context35111>();
using var context = contextFactory.CreateContext();

var data = async
? await context.Set<FooBar35111>().ToListAsync()
: context.Set<FooBar35111>().ToList();
}

protected class Context35111(DbContextOptions options) : DbContext(options)
{
public int Foo { get; set; }
public long? Bar { get; set; }
public List<long> Baz { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<FooBar35111>()
.HasQueryFilter(e =>
Foo == 1
? Baz.Contains(e.Bar)
: e.Bar == Bar);
}
}

public class FooBar35111
{
public long Id { get; set; }
public long Bar { get; set; }
}

#endregion
}

0 comments on commit c7e1061

Please sign in to comment.