Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice calling scalar functions with Entity Framework Core (2.1)

I often need to call scalar functions that are defined on a SQL Server from my web applications (ASP.NET Core / EF Core). Since these functions are just simple helper functions and I also use a lot of them I use a general pattern for calling these scalar functions - with the help of the new query types available from EF Core 2.1. Since I am relatively new to EF Core my question is if this pattern might cause problems and/or if there is a better solution or best practice for calling scalar functions. The solution works and I cannot observe any problems so far but for example I wondered if using the same query type for different functions might lead to unexpected values or weird behaviour due to caching/tracking behaviour, etc. within EF Core - it's more of a gut feeling.

So here's the pattern: Instead of defining different entity types for every single scalar function I simply define one generic type:

public class PrimitiveDto<T>
{
    public T Value { get; set; }
}

In my context class I register these types for every return type I expect from the scalar functions I want to use - so for all scalar functions returning 'int' the context class would have one additional entry like this:

public virtual DbQuery<PrimitiveDto<int>> BasicIntDto { get; set; }

For EF Core >= 3 it is:

public virtual DbSet<PrimitiveDto<int>> BasicIntDto { get; set; }

In every part of the application where I want to call a scalar function returning 'int' I simply use the same following pattern:

context.BasicIntDto.FromSql("SELECT <FUNCTION> AS Value")

By using this pattern I can call any number of functions the same way without defining additional types or extending the context class.

Please let me know if I could run into a trap through this pattern. Thank you very much.

like image 982
Grimm Avatar asked Jul 03 '18 10:07

Grimm


1 Answers

Unfortunately it seems this feature has been left aside: https://github.com/aspnet/EntityFrameworkCore/issues/9810

An option is to wrap the functions calls in a static class using a small table which is never empty:

public static class DbFunctions
{
   public static decimal MyFunctionABC(int param1, int param2)
   {
       using (var db = new MyDbContext())
       {
        return db.table.Take(1).Select(t => MyDbContext.MyFunctionABC(x, y)).Single();
       }
    }
 }

Then you can call DbFunctions.MyFunctionABC(x,y);

like image 152
sofsntp Avatar answered Nov 04 '22 07:11

sofsntp