Skip to content

AsSingleQuery can not be translated by in-memory provider #23759

@Cherepoc

Description

@Cherepoc

AsSingleQuery does not work with in-memory provider. Interestingly, AsSplitQuery does work.

Here's fiddler:
https://dotnetfiddle.net/9LG89a

Here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
					
public class Program
{
	public static void Main()
	{
		var options = new DbContextOptionsBuilder<ApplicationDbContext>()
			.UseInMemoryDatabase(Guid.NewGuid().ToString())
			.Options;
		using var dbContext = new ApplicationDbContext(options);
		
		var parent = new ParentEntity();
		parent.Children.Add(new ChildEntity());
		dbContext.Add(parent);
		dbContext.SaveChanges();
		
		var result = dbContext.Parents
			.Select(parent => parent.Children.Select(child => child.Id))
			.AsSingleQuery()
			.ToList();
			
		Console.WriteLine("Success");
	}
	
	public sealed class ApplicationDbContext : DbContext
	{
		public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
		{
		}
		
		public DbSet<ParentEntity> Parents {get;set;}
		
		public DbSet<ChildEntity> Children {get;set;}
	}
	
	public sealed class ParentEntity
	{
		public int Id {get;set;}
		public ICollection<ChildEntity> Children {get;set;} = new HashSet<ChildEntity>();
	}
	
	public sealed class ChildEntity
	{
		public int Id {get;set;}
		public int ParentEntityId {get;set;}
		public ParentEntity ParentEntity {get;set;}
	}
}

Here's the error with stack trace:

Unhandled exception. System.InvalidOperationException: The LINQ expression 'DbSet<ParentEntity>()
    .Select(p0 => DbSet<ChildEntity>()
        .Where(c => EF.Property<Nullable<int>>(p0, "Id") != null && object.Equals(
            objA: (object)EF.Property<Nullable<int>>(p0, "Id"), 
            objB: (object)EF.Property<Nullable<int>>(c, "ParentEntityId")))
        .Select(c => c.Id))
    .AsSingleQuery()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.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.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Program.Main()

EF Core version: 5.0.1
Database provider: Microsoft.EntityFrameworkCore.InMemory
Target framework: NET 5.0
Operating system: Windows 10 Pro 2004
IDE: Rider 2020.3

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions