JetBrains Rider 2024.1 Help

Code inspection: Function is not convertible to SQL and must not be called in the database context

Entity Framework tries to translate methods in a LINQ query into SQL that will be executed in DBMS. But not all .NET methods are supported. When a method is called that is not supported by LINQ to Entities, the call fails at runtime and typically a NotSupportedException is thrown

To fix this problem, make sure that only those parts of the LINQ query that are translatable to SQL are used within the scope of the database context. Other parts should be used after the query and executed in-memory:

In the example below, a non-translatable method Reverse() is called inside a Where() clause, which is directly tied to the database context. When the Entity Framework tries to resolve this LINQ query, it must translate the Where() clause and everything inside to SQL, and fails, because there is no SQL equivalent to Reverse().

A fix in this case would be to move AsEnumerable() and the execution context of the query from the database IQueryable to in-memory IEnumerable:

public static void Test() { var context = new MyContext(); foreach (var person in context.People.Where(person => person.Accounts.AsEnumerable().Reverse().Any())) { Console.WriteLine(person); } }
public static void Test() { var context = new MyContext(); foreach (var person in context.People.AsEnumerable().Where(person => person.Accounts.Reverse().Any())) { Console.WriteLine(person); } }
Last modified: 08 April 2024