This example shows how to use a stored function that performs operations and returns a scalar value.
The following SQL Server script creates the stored function.
CREATE FUNCTION dbo.Sum(@p1 int, @p2 int)
RETURNS int
AS
BEGIN
RETURN @p1 + @p2;
END
GO
Create a model and add a method that maps to the stored function.
You can do this during model creation on the Select Database Objects page of the Create Model Wizard, or by dragging the required stored function from Database Explorer to the diagram or Model Explorer in an existing model.
As a result, the model includes a method that corresponds to the stored function.

Method parameters are as follows.

As a result of code generation, the following method is generated with a signature close to the stored function.
C#:
public System.Nullable<int> Sum(System.Nullable<int> p1, System.Nullable<int> p2)
{
OAParameter p1Parameter = new OAParameter();
p1Parameter.ParameterName = @"p1";
p1Parameter.Direction = ParameterDirection.Input;
if (p1.HasValue)
{
p1Parameter.Value = p1.Value;
}
else
{
p1Parameter.DbType = DbType.Int32;
p1Parameter.Size = -1;
p1Parameter.Value = DBNull.Value;
}
OAParameter p2Parameter = new OAParameter();
p2Parameter.ParameterName = @"p2";
p2Parameter.Direction = ParameterDirection.Input;
if (p2.HasValue)
{
p2Parameter.Value = p2.Value;
}
else
{
p2Parameter.DbType = DbType.Int32;
p2Parameter.Size = -1;
p2Parameter.Value = DBNull.Value;
}
OAParameter returnValueParameter = new OAParameter();
returnValueParameter.Direction = ParameterDirection.ReturnValue;
returnValueParameter.DbType = DbType.Int32;
returnValueParameter.Size = -1;
this.ExecuteNonQuery(@"dbo.[Sum]", CommandType.StoredProcedure, p1Parameter, p2Parameter, returnValueParameter);
System.Nullable<int> result = (System.Nullable<int>)returnValueParameter.Value;
return result;
}
You can now call the stored function in your application through the generated method wrapper. The wrapper provides a consistent way to work with the function in code.
Wrapper methods are strongly typed, appear in IntelliSense, and have signatures that match the corresponding stored functions.