Entity Framework Core Pitfalls: Calling DB Functions in LINQ Queries as Extension Methods
Introduction Another one for my series on Entity Framework Core Pitfalls. Sometimes, even when using EF Core, we need to perform some operations, like calling a database (DB) function, that is not mapped to LINQ. We've been able to do this for a long time, and it is certainly the case with EF Core. Let's see how. Calling DB Functions in LINQ Queries So, some standard DB functions have been mapped to EF.Functions , as part of the DbFunctions class, but there are a big number of them that haven’t. Some examples include SQL Server's REVERSE , SOUNDEX , DIFFERENCE , and many others. Now, there's a way to call them in LINQ queries, but there is a (minor) caveat. One way to do this is to have a method in your DbContext -derived class, which can be static or instance, such as: public string Reverse(string property) => throw new NotImplementedException("Only to be called in an EF Core LINQ query."); }; Now, we need to tell EF Core to use this function, and there...