Efcore: Built In Scalar Function Mapping

Created on 5 Dec 2019  路  4Comments  路  Source: dotnet/efcore


For example, if EF Core 2.2 couldn鈥檛 translate a predicate in a Where() call, it executed an SQL statement without a filter, transferred all the rows from the database, and then filtered them in-memory:

var specialCustomers = 
  context.Customers
    .Where(c => c.Name.StartsWith(n) && IsSpecialCustomer(c));

That may be acceptable if the database contains a small number of rows but can result in significant performance issues or even application failure if the database contains a large number or rows.
To evaluate a predicate condition on the client as in the previous example, developers now need to explicitly switch evaluation of the query to LINQ to Objects:


var special customers =
  context.Customers
    .Where(c => c.Name.StartsWith(n)) 
    .AsEnumerable() // switches to LINQ to Objects
    .Where(c => IsSpecialCustomer(c));

EntityFramework Core, Create and Map , scalar functions in the database to complicated builtIn
methods and well-known methods used by strings and numbers ... , and allow developers to easily create and map scalar function, to methods. So a lot of client-side evaluation will be evaluated in the database.

closed-question customer-reported

Most helpful comment

@tlejmi I'm still not clear on what you're asking. EF Core knows how to translate certain methods and members, and more translations can be added if they are needed. Adding translations is not an automatic/transparent thing, and must be done manually for each case.

Are you specifically asking that we add translation support for string.Reverse? If so, you open an issue specifically for that. Otherwise you can take a look at this class, which defines all the string method translations

All 4 comments

@tlejmi can you clarify exactly what you're asking for?

If you're looking to define a custom function and map it to SQL, EF Core already fully supports that. We're lacking proper documentation for this (https://github.com/aspnet/EntityFramework.Docs/issues/500), but see this comment with samples.

what I meant is that Entity Framework will map by default c# built-in functions.
Like mapping c# string.Reverse() to TSQL REVERSE ( string_expression ) so that the reverse function will not be evaluated in client-side . and the same for the well-known number and string functions used as filters
Similar to this : https://entityframeworkcore.com/knowledge-base/51896918/ef-core-linq-use-scalar-function

@tlejmi I'm still not clear on what you're asking. EF Core knows how to translate certain methods and members, and more translations can be added if they are needed. Adding translations is not an automatic/transparent thing, and must be done manually for each case.

Are you specifically asking that we add translation support for string.Reverse? If so, you open an issue specifically for that. Otherwise you can take a look at this class, which defines all the string method translations

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

Was this page helpful?
0 / 5 - 0 ratings