-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Closed as not planned
Closed as not planned
Copy link
Labels
Description
Bug description
modelBuilder.Entity().ToInMemoryQuery( () => ... ) cannot be translated to SQL in EF Core version 10.
It works fine in the previous EF Core version 9.
We use the functionality to create grafted "views" for the curated model used by our HotChocolate GraphQL application.
Your code
// <Project Sdk="Microsoft.NET.Sdk">
// <PropertyGroup>
// <OutputType>Exe</OutputType>
// <TargetFramework>net10.0</TargetFramework>
// <ImplicitUsings>enable</ImplicitUsings>
// <Nullable>enable</Nullable>
// </PropertyGroup>
// <ItemGroup>
// <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.1" />
// <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.1" />
// </ItemGroup>
// </Project>
using Microsoft.EntityFrameworkCore;
using ( var ctx = new TableContext() ) {
var tables = ctx.EmbracedTables.Take(10).ToList();
foreach ( var table in tables ) {
Console.WriteLine( table.Name );
}
}
class EmbracedTables {
public string Name { get; set; }
public int Object_id { get; set; }
}
class Tables {
public string Name { get; set; }
public int Object_id { get; set; }
}
class TableContext : DbContext {
public DbSet<EmbracedTables> EmbracedTables { get; set; }
public DbSet<Tables> Tables { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder
.UseSqlServer("Server=localhost;Database=WideWorldImporters; integrated security=true; trusted_connection=true; trustServerCertificate=true")
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
.LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Debug);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("sys");
modelBuilder.Entity<Tables>().HasKey(e => e.Object_id);
modelBuilder.Entity<EmbracedTables>().HasKey(e => e.Object_id);
modelBuilder.Entity<EmbracedTables>().ToInMemoryQuery( () =>
from t in Tables
select new EmbracedTables { Name = "{" + t.Name + "}", Object_id = t.Object_id } );
}
}Stack traces
Unhandled exception. System.InvalidOperationException: The LINQ expression '@ef_filter__Tables
.Select(t => new EmbracedTables{
Name = "{" + t.Name + "}",
Object_id = t.Object_id
}
)' 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.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorExpression[TResult](Expression query)
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__DisplayClass11_0`1.<ExecuteCore>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
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>$(String[] args) in C:\Projects\Xplore\EfCoreToInmMemoryBug\Program.cs:line 20
Verbose output
dbug: 19-12-2025 13:33:10.379 CoreEventId.ContextInitialized[10403] (Microsoft.EntityFrameworkCore.Infrastructure)
Entity Framework Core 10.0.1 initialized 'TableContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:10.0.1' with options: EngineType=SqlServer NoTracking
dbug: 19-12-2025 13:33:10.398 CoreEventId.QueryCompilationStarting[10111] (Microsoft.EntityFrameworkCore.Query)
Compiling query expression:
'DbSet<EmbracedTables>()
.Take(@p)'
Unhandled exception. System.InvalidOperationException: The LINQ expression '@ef_filter__Tables
.Select(t => new EmbracedTables{
Name = "{" + t.Name + "}",
Object_id = t.Object_id
}
)' 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.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorExpression[TResult](Expression query)
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__DisplayClass11_0`1.<ExecuteCore>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
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>$(String[] args) in C:\Projects\Xplore\EfCoreToInmMemoryBug\Program.cs:line 20
dbug: 19-12-2025 13:33:10.441 CoreEventId.ContextDisposed[10407] (Microsoft.EntityFrameworkCore.Infrastructure)
'TableContext' disposed.
EF Core version
10.0.1
Database provider
Microsoft.EntityFrameworkCore.InMemory
Target framework
.NET 10.0.101
Operating system
No response
IDE
Visual Studio Code 1.107.1
gsc-doge and h0wXD