This example shows how to use a stored function that executes some operations and returns a scalar value as a result.
The script for creating a stored function for the SQL Server DBMS is as follows:
CREATE FUNCTION dbo.Sum(@p1 int, @p2 int)
RETURNS int
AS
BEGIN
RETURN @p1 + @p2;
END
GO
First, we create a model and drag the corresponding stored function from the Database Explorer window to the Stored Procedures node in the Model Explorer window.
After this, we select the respective function in the Stored Procedures node and in the Properties window locate the Concealed Function property and set its value to True; the Composable property value is automatically set to False.
Then we right-click the required stored function in the Stored Procedures node and select the Create Method option in the context menu.
As a result, we get a method in the model corresponding to the stored function:
With the following parameters:
As a result of code generation for the model, the corresponding method of the model context will be generated having a signature close to the relevant stored function:
C#:
public global::System.Nullable<int> Sum (global::System.Nullable<int> p1, global::System.Nullable<int> p2)
Visual Basic:
Public Function Sum (ByVal p1 As Global.System.Nullable(Of Integer), ByVal p2 As Global.System.Nullable(Of Integer)) As Byte()
Now it is possible to use this stored function in the application with the help of method wrapper. This allows working with the stored function with all possible convenience, as wrapper methods are strongly typed, are found by IntelliSense and have signatures close to the corresponding stored functions.