Output parameter in stored procedure in EF

后端 未结 2 995
迷失自我
迷失自我 2020-12-09 18:32

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
相关标签:
2条回答
  • 2020-12-09 19:14

    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.

    0 讨论(0)
  • 2020-12-09 19:24

    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);
    }
    
    0 讨论(0)
提交回复
热议问题