Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write EF.Functions extension method?

I see that EF Core 2 has EF.Functions property EF Core 2.0 Announcement which can be used by EF Core or providers to define methods that map to database functions or operators so that those can be invoked in LINQ queries. It included LIKE method that gets sent to the database.

But I need a different method, SOUNDEX() that is not included. How do I write such a method that passes the function to the database the way DbFunction attribute did in EF6? Or I need to wait for MS to implement it? Essentially, I need to generate something like

SELECT * FROM Customer WHERE SOUNDEX(lastname) = SOUNDEX(@param)
like image 330
Felix Avatar asked Sep 14 '17 07:09

Felix


People also ask

What is EF method?

The Environmental Footprint (EF) is an initiative of the European Commission, establishing a common methodological approach for quantifying the environmental performance of any good or service throughout its life cycle.

How do you call a function in Entity Framework?

Step 1: Create an entity class which inherits “DbContext” class. Step 2: The following is the structure of the database with table and stored procedure. Step 3: Create a class to store the returned tabular value. Step 4: Create an object for the entity above and method to call a function.

How do I use functions in EF core?

EF Core allows for using user-defined SQL functions in queries. To do that, the functions need to be mapped to a CLR method during model configuration. When translating the LINQ query to SQL, the user-defined function is called instead of the CLR function it has been mapped to.

Which of the following method is used to execute the stored procedure in EF core?

EF Core provides the following methods to execute a stored procedure: DbSet<TEntity>. FromSql()


2 Answers

Adding new scalar method to EF.Functions is easy - you simply define extension method on DbFunctions class. However providing SQL translation is hard and requires digging into EFC internals.

However EFC 2.0 also introduces a much simpler approach, explained in Database scalar function mapping section of the New features in EF Core 2.0 documentation topic.

According to that, the easiest would be to add a static method to your DbContext derived class and mark it with DbFunction attribute. E.g.

public class MyDbContext : DbContext
{
    // ...

    [DbFunction("SOUNDEX")]
    public static string Soundex(string s) => throw new Exception();
}

and use something like this:

string param = ...;
MyDbContext db = ...;
var query = db.Customers
    .Where(e => MyDbContext.Soundex(e.LastName) == MyDbContext.Soundex(param));

You can declare such static methods in a different class, but then you need to manually register them using HasDbFunction fluent API.

like image 190
Ivan Stoev Avatar answered Sep 29 '22 03:09

Ivan Stoev


EFC 3.0 has changed this process a little, as per https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#udf-empty-string

Example of adding CHARINDEX in a partial context class:

public partial class MyDbContext
{
    [DbFunction("CHARINDEX")]
    public static int? CharIndex(string toSearch, string target) => throw new Exception();

    partial void OnModelCreatingPartial(
        ModelBuilder modelBuilder)
    {
        modelBuilder
            .HasDbFunction(typeof(MyDbContext).GetMethod(nameof(CharIndex)))
            .HasTranslation(
                args =>
                    SqlFunctionExpression.Create("CHARINDEX", args, typeof(int?), null));
    }
}
like image 20
Wayne Cornish Avatar answered Sep 29 '22 04:09

Wayne Cornish