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

前端 未结 1 1515
余生分开走
余生分开走 2021-02-13 12:31

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

1条回答
  •  -上瘾入骨i
    2021-02-13 13:08

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

    0 讨论(0)
提交回复
热议问题