How to call DB function from EF LINQ query?

前端 未结 2 1508
遥遥无期
遥遥无期 2020-12-19 06:56

In my database I have define some function, let\'s say it\'s called fnTest. Is it possible to call that function from my LINQ/EF query? Something like this:

2条回答
  •  轮回少年
    2020-12-19 07:52

    First off, fnTest will have to be created as a user-defined function in the database first:

    CREATE FUNCTION [fnTest] (@fi int, @f2 int, @param3 int, @param4 int)
    RETURNS int
    AS ...
    

    Then in your .edmx file, declare the function like this:

    
        
        
        
        
    
    

    Now you can bind this function to a method in your model like this:

    [EdmFunction("MyNamespace", "fnTest")]
    public static int fnTest(int f1, int f2, int param3, int param4)
    {
        throw new NotSupportedException("Direct calls are not supported.");
    }
    

    You can now use this method in standard LINQ queries.

    Further Reading

    • How to: Call Custom Database Functions

提交回复
热议问题