InMemory provider v7.x.x: FromSqlRaw throws InvalidOperationException: Query root of type 'FromSqlQueryRootExpression' wasn't handled by provider code.

See original GitHub issue

After upgrading to EFCore 7, Microsoft.EntityFrameworkCore.InMemory provider is throwing an error for ‘FromSqlRaw’ \ ‘FromSqlInterpolated’ expressions.

The same scenario is working properly in EF Core 3\5\6 and does not throw the error.

var dbContextOptions = new DbContextOptionsBuilder<SampleDbDataContext>()
	.UseInMemoryDatabase("SampleDb_in-memory-database")
	.Options;

using var dataContext = new SampleDbDataContext(dbContextOptions);

var fooBars = await dataContext.FooBars
	.FromSqlRaw
	(
		@"
		-- Real queries are more complicated, i.e. include JSON \ XML parsing,
		-- calls of aggregate or user-defined functions etc.

		SELECT
			[Id],
			[Value]
		FROM [dbo].[FooBar]
		"
	).ToListAsync();

Zip archive with a solution that reproduces the issue: https://1drv.ms/u/s!AiXcux4pieqkhfMOviZNjCeS96ZJLQ?e=1ge3cE

The code above throws the error:

System.InvalidOperationException: Query root of type 'FromSqlQueryRootExpression' wasn't handled by provider code. This issue happens when using a provider specific method on a different provider where it is not supported.
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryQueryableMethodTranslatingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at System.Linq.Expressions.Expression.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__DisplayClass12_0`1.<ExecuteAsync>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)

EF Core version: v7 Database provider: Microsoft.EntityFrameworkCore.InMemory v7.x.x Target framework: .NET 6.0 IDE: Visual Studio 2022 17.2.8

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
davidrothcommented, Feb 10, 2023

The InMemoryProvider cannot evaluate sql queries, which makes sense because by definition it’s not a real database.

So the exception makes total sense here and I don’t know what your are expecting to change?

Did EF6 silently return an empty result in such a scenario? If so, I’d argue the new exception behavior is favourable because it’s indicating you that you are doing something wrong.

0reactions
ajcvickerscommented, Feb 16, 2023

Note from triage: the documentation for this break does not include the in-memory provider. Opened https://github.com/dotnet/EntityFramework.Docs/issues/4257 to add in-memory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework query root of type ' ...
EntityID); I get: System.InvalidOperationException : Query root of type 'FromSqlQueryRootExpression' wasn't handled by provider code. This issue ...
Read more >
Throw when using relational FromSqlRaw with Cosmos, or ...
5". The error is: Code CS0121 The call is ambiguous between the following methods or properties: 'Microsoft.EntityFrameworkCore.
Read more >
RelationalQueryableExtensions.FromSql Method
Creates a LINQ query based on an interpolated string representing a SQL query.
Read more >
Executing Raw SQL Queries using FromSql Method
The FromSql method in Entity Framework Core is used to execute raw SQL queries against the database and return the results as entities....
Read more >
Avoid In-Memory Databases for Tests
Any ORM maintainer can tell you - the code in the query provider can dwarf the code in the rest of the codebase....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found