efcore icon indicating copy to clipboard operation
efcore copied to clipboard

FirstOrDefaultAsync is not supported in EF.CompileQuery

Open Meberem opened this issue 5 years ago • 8 comments

FirstOrDefaultAsync is not supported when calling a Compiled Query

Exception message:
Stack trace: System.NotSupportedException: Could not parse expression 'value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MyEntity]).Where(m => (((m.One == __request.One) AndAlso (m.Two == __request.Two)) AndAlso (m.Three == __request.Three))).Select(m => m.Id).FirstOrDefaultAsync(__cancellationToken)': This overload of the method 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstOrDefaultAsync' is currently not supported.

Steps to reproduce

Run the query

public class Query
{
    public int One { get; set; }
    public int Two { get; set; }
    public int Three { get; set; }
}

public class MyEntity
{
    public int One { get; set; }
    public int Two { get; set; }
    public int Three { get; set; }
    public int Four { get; set; }
}

public class MyDbContext : DbContext
{
    public DbSet<MyEntity> MyEntities { get; set; }
}

private static readonly Func<MyDbContext, Query, CancellationToken, Task<string>>
            PreCompiledQuery = EF.CompileQuery((MyDbContextcontext, Query request, CancellationToken cancellationToken) =>
                context
                    .MyEntities
                    .Where(m => m.One == request.One &&
                                m.Two == request.Two &&
                                m.Three == request.Three)
                    .Select(m => m.Four)
                    .FirstOrDefaultAsync(cancellationToken));

Further technical details

EF Core version: Microsoft.EntityFrameworkCore.SqlServer (2.1.4) Operating system: Window 10 IDE: Visual Studio 2017 15.8.4

Meberem avatar Oct 10 '18 09:10 Meberem