Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to use EF Core's DbSet<>.FromSqlRaw along with ToLinqToDB()?

See original GitHub issue

I have an application that bases some result sets on raw SQL queries for an entity. The IQueryable returned from DbSet<>.FromSqlRaw(“select * from tablename”) for example fails when attempting to enumerate the IQueryable then returned from ToLinqToDB(). Is this situation supported? I was hoping it would use a subquery to apply the additional filters added by users of the IQueryable returned from ToLinqToDB().

I’m using linq2db.EntityFrameworkCore 2.0.5, linq2db 2.9.7, EFCore 3.1.3 against SQL Server 2017.

I receive the following exception and stack trace:

The method or operation is not implemented.
  at Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSqlOnQueryable[TEntity](IQueryable`1 source, String sql, Object[] parameters)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
 at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
 at LinqToDB.EntityFrameworkCore.LinqToDBForEFToolsImplDefault.<>c__DisplayClass35_0.<TransformExpression>g__LocalTransform|2(Expression e)
 at LinqToDB.EntityFrameworkCore.LinqToDBForEFToolsImplDefault.<>c__DisplayClass35_0.<TransformExpression>b__3(Expression e)
 at LinqToDB.Expressions.Extensions.Transform(Expression expr, Func`2 func)
 at LinqToDB.EntityFrameworkCore.LinqToDBForEFToolsImplDefault.TransformExpression(Expression expression, IDataContext dc, DbContext ctx, IModel model)
 at LinqToDB.EntityFrameworkCore.LinqToDBForEFTools.TransformExpression(Expression expression, IDataContext dc, DbContext ctx, IModel model)
 at LinqToDB.EntityFrameworkCore.LinqToDBForEFToolsDataContext.ProcessExpression(Expression expression)
 at LinqToDB.Linq.Query`1.GetQuery(IDataContext dataContext, Expression& expr)
 at LinqToDB.Linq.ExpressionQuery`1.GetQuery(Expression& expression, Boolean cache)
 at LinqToDB.Linq.ExpressionQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
 at LinqToDB.EntityFrameworkCore.Internal.LinqToDBForEFQueryProvider`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
 at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
 at Redacted.HealthCheckController.Get() in C:\redacted\HealthCheckController.cs:line 36
 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
 at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<<InvokeActionMethodAsync>g__Logged|12_1>d.MoveNext()

I’m calling a method on my data context that does the following:

public virtual IQueryable<CapacityData> GetRunsReport()
            return CapacityRows.FromSqlRaw("select * from API_CapacityData_t where ProcessUnitCode IN (200, 201, 202, 203, 204, 205, 206)");

and then, and an example, doing this with that context: _dc.GetRunsReport().ToLinqToDB().ToList();

I understand this scenario might not be intended as supported, but in the case it is supposed to be handled, I’m opening this issue.

Thank you!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

djkunkelcommented, Apr 16, 2020

Thank you for this guidance. I was able to make my change in a custom override of TransformExpression!

sdanylivcommented, Apr 8, 2020

Will find time to convert FromSqlRaw. We have our own FromSql method. Just forgot to create transformation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Execute custom SQL before running FromSqlRaw in Entity ...
Personally I would define a query type in EF Core to represent the TVP. Then you can use raw sql to return an...
Read more >
SQL Queries - EF Core
Using SQL queries in Entity Framework Core. ... FromSql can only be used directly on a DbSet . It cannot be composed over...
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 >
linq2db.EntityFrameworkCore is an integration of LINQ To DB with existing EntityFrameworkCore projects. It was inspired by this issue in EF.Core repository.
Read more >
Raw SQL Queries in EF-Core - Learn Entity Framework Core 7
In Entity Framework Core, if your LINQ Query is not able to execute the application properly, then there is an option for embedding...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found