Basic `EF.CompileAsyncQuery` Results in `System.ArgumentException`: 'Argument types do not match'
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
## File a bug
I appear to be running into a bug with `EF.CompileAsyncQuery` when returning an instance object/result vs `IQueryable` (which has worked amazingly well).
When returning an instance object/result, I run into an `System.ArgumentException` with the simplest of expressions.
### Include your code
You should be able to load and hit F5 on this solution here:
https://github.com/Mike-E-angelo/Stash/blob/master/EfCore.CompiledQueries.BasicExpression/EfCore.CompiledQueries.BasicExpression.sln
Error is encountered here:
https://github.com/Mike-E-angelo/Stash/blob/master/EfCore.CompiledQueries.BasicExpression/EfCore.CompiledQueries.BasicExpression/Worker.cs#L34
```C#
var query = EF.CompileAsyncQuery(x => new Statistic
{
Day = x.Subjects.Count()
});
await query(context); // System.ArgumentException: 'Argument types do not match'
sealed class Context : DbContext
{
public Context(DbContextOptions options) : base(options) {}
public DbSet Subjects { get; set; } = default!;
}
sealed class Subject
{
public Guid Id { get; set; }
public string Name { get; set; } = default!;
}
public class Statistic
{
public int Day { get; set; }
public int Week { get; set; }
public long All { get; set; }
}
```
### Include stack traces
Include the full exception message and stack trace for any exception you encounter.
Use triple-tick fences for stack traces. For example:
```
System.ArgumentException
HResult=0x80070057
Message=Argument types do not match
Source=System.Linq.Expressions
StackTrace:
at System.Linq.Expressions.Expression.Bind(MemberInfo member, Expression expression)
at System.Linq.Expressions.MemberAssignment.Update(Expression expression)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
at System.Linq.Expressions.MemberInitExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CreateCompiledAsyncQuery[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledAsyncTaskQuery`2.CreateCompiledQuery(IQueryCompiler queryCompiler, Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryBase`2.<>c.b__6_0(CompiledQueryBase`2 t, TContext c, LambdaExpression q)
at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam1,TParam2,TParam3,TValue](TValue& target, TParam1 param1, TParam2 param2, TParam3 param3, Func`4 valueFactory)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryBase`2.EnsureExecutor(TContext context)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryBase`2.ExecuteCore(TContext context, CancellationToken cancellationToken, Object[] parameters)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryBase`2.ExecuteCore(TContext context, Object[] parameters)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledAsyncTaskQuery`2.ExecuteAsync(TContext context)
at EfCore.CompiledQueries.BasicExpression.Worker.d__3.MoveNext() in ...\Mike-E-angelo\Stash\EfCore.CompiledQueries.BasicExpression\EfCore.CompiledQueries.BasicExpression\Worker.cs:line 34
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at EfCore.CompiledQueries.BasicExpression.Worker.d__3.MoveNext() in ...\Mike-E-angelo\Stash\EfCore.CompiledQueries.BasicExpression\EfCore.CompiledQueries.BasicExpression\Worker.cs:line 37
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at Microsoft.Extensions.Hosting.Internal.Host.d__12.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.d__4.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.d__4.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at EfCore.CompiledQueries.BasicExpression.Program.(String[] args)
This exception was originally thrown at this call stack:
[External Code]
EfCore.CompiledQueries.BasicExpression.Worker.StartAsync(System.Threading.CancellationToken) in Worker.cs
[External Code]
EfCore.CompiledQueries.BasicExpression.Worker.StartAsync(System.Threading.CancellationToken) in Worker.cs
[External Code]
```
### Include verbose output
NA
### Include provider and version information
EF Core version: `6.0.3`
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) `Microsoft.EntityFrameworkCore.SqlServer`
Target framework: (e.g. .NET 5.0) `net6.0`
Operating system: Windows 10
IDE: (e.g. Visual Studio 2019 16.3) Visual Studio 2022 17.1 RTM
Contributor guide
Assessment
This issue has not been assessed yet.