Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Cherepoc opened this issue Dec 23, 2020 · 2 comments
Closed

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

Cherepoc opened this issue Dec 23, 2020 · 2 comments
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@Cherepoc
Copy link

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

@anranruye

This comment has been minimized.

@Cherepoc
Copy link
Author

No, but this provider is used for testing the code that is written for other providers, so at least it should not throw exceptions.

@ajcvickers ajcvickers added this to the 5.0.3 milestone Jan 4, 2021
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 5, 2021
smitpatel added a commit that referenced this issue Jan 5, 2021
This was referenced Mar 6, 2021
This was referenced Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

4 participants