Repository navigation
Description
In compiled queries, we don't perform client evaluation over uncorrelated expressions (which don't reference database objects) like we do in regular queries. This means we get queries with primitive collections where the elements are never compared to some other column, and therefore we have no way of inferring the collection's type mapping:
Func<ApplicationDbContext, string[], IAsyncEnumerable<Currency>> func
= EF.CompileAsyncQuery((ApplicationDbContext context, string[] search) => context.Currencies.Where(e => search.Any()));
_ = await func(context, []).ToListAsync();
// Non-compiled version works:
// string[] search = [];
// _ = await context.Currencies.Where(e => search.Any()).ToListAsync();This causes e.g. an exception in RelationalTypeMappingPostprocessor.ApplyTypeMappingsOnValuesExpression.
#19749 would make us perform client evaluation over uncorrelated expressions, so they'd behave like non-compiled queries; at that point, search.Any() would be evaluated client-side and we'd no longer end up with a collection. However, that issue is complex, and at least in theory we could have such cases in the tree for other reasons.
Originally raised by @Enderlook in PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#2023.
Full minimal repro
await using var context = new ApplicationDbContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
Func<ApplicationDbContext, string[], IAsyncEnumerable<Currency>> func
= EF.CompileAsyncQuery((ApplicationDbContext context, string[] search) => context.Currencies.Where(e => search.Any()));
_ = await func(context, []).ToListAsync();
// Non-compiled version works:
// string[] search = [];
// _ = await context.Currencies.Where(e => search.Any()).ToListAsync();
public class ApplicationDbContext : DbContext
{
public DbSet<Currency> Currencies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(Environment.GetEnvironmentVariable("Test__SqlServer__DefaultConnection"))
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Currency
{
public int Id { get; set; }
public string Name { get; set; }
}
Activity
Fix uncorrelated parameterized collections in compiled queries
Fix uncorrelated parameterized collections in compiled queries
Fix uncorrelated parameterized collections in compiled queries
Fix uncorrelated parameterized collections in compiled queries
Fix uncorrelated parameterized collections in compiled queries (dotne…
A compiled query that consumes its list runs EF's statement, or fails…
A compiled query that consumes its list runs EF's statement, or fails…