Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output parameter in stored procedure in EF

I have an existing database with lots of complex stored procedure and I want to use those procedure through EF 4. I have done the following:

  1. Created an EF data object, Customer.
  2. Added a Stored Procedure into the EF
  3. Right Click on the EF designer and add a function import.
  4. Function Import Name - MyFunction, complex type.

Resulting code:

CustomerEntities entity = new CustomerEntities();
var result = entity.MyFunction("XYZ", ref o_MyString);

Now my stored procedure has an output parameter which I used to call by the ref (in WebForm). But I am getting the below error:

cannot convert from 'ref string' to 'System.Data.Objects.ObjectParameter'

Please help

Edit

When I am trying to save I am getting the below error

A mapping function binding specifies a function Model.Store.P_GetCustomer with an unsupported parameter: o_MyString. Output parameters may only be mapped through the RowsAffectedParameter property. Use result bindings to return values from a function invocation.

like image 490
Chris Avatar asked May 31 '11 20:05

Chris


People also ask

Can stored procedure have output parameter?

The Output Parameters in Stored Procedures are used to return some value or values. A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.

What is output parameter in stored procedure?

Output parameter is a parameter whose value is passed out of the stored procedure/function module, back to the calling PL/SQL block. An OUT parameter must be a variable, not a constant. It can be found only on the left-hand side of an assignment in the module.

How do you pass an output parameter to a procedure?

To call a stored procedure with output parameters, you follow these steps: First, declare variables to hold the values returned by the output parameters. Second, use these variables in the stored procedure call.


2 Answers

Output parameters are returned in ObjectParameter instance. So you must use code like:

var oMyString = new ObjectParameter("o_MyString", typeof(string));
var result = ctx.MyFunction("XYZ", oMyString).ToList();
var data = oMyString.Value.ToString();

The reason is that function import cannot use ref parameter because output parameter is not filled until you process result set from the database = if you don't call ToList or iterate the result of the stored procedure the output parameter is null.

like image 198
Ladislav Mrnka Avatar answered Oct 08 '22 13:10

Ladislav Mrnka


msdn suggests the following:

CREATE PROCEDURE dbo.GetDepartmentName
     @ID INT ,
     @Name NVARCHAR(50) OUTPUT
AS
     SELECT   @Name = Name
     FROM     Department
     WHERE    DepartmentID = @ID

Solution

using (SchoolEntities context = new SchoolEntities())
{
   // name is an output parameter.

   ObjectParameter name = new ObjectParameter("Name", typeof(String));
   context.GetDepartmentName(1, name);
   Console.WriteLine(name.Value);
}
like image 22
Faisal Avatar answered Oct 08 '22 15:10

Faisal