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

青春壹個敷衍的年華 提交于 2019-12-21 03:52:27

问题


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; }

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.


回答1:


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);



来源:https://stackoverflow.com/questions/51151773/best-practice-calling-scalar-functions-with-entity-framework-core-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!