Executing SQL Stored Procedure with Output Parameter from Entity Framework

眉间皱痕 提交于 2019-12-17 19:20:46

问题


Using EF, I'm trying to execute a stored procedure that returns a single string value, i.e. the status of an SQL Agent Job.

The stored procedure is declared as

CREATE PROCEDURE [dbo].[up_GetJobStatus](@JobStatus NVARCHAR(30) OUTPUT)
AS

-- some code omitted for brevity

SELECT @JobStatus = (
SELECT  
    CASE job_state 
    WHEN 1 THEN 'Executing'
    WHEN 2 THEN 'Waiting for thread'
    WHEN 3 THEN 'Between retries'
    WHEN 4 THEN 'Idle'
    WHEN 5 THEN 'Suspended'
    WHEN 6 THEN '<unknown>'
    WHEN 7 THEN 'Performing completion actions'
END
FROM @xp_results results 
INNER JOIN msdb.dbo.sysjobs sj
ON results.job_id = sj.job_id
WHERE sj.job_id = @job_id)

RETURN

I have verified the stored procedure is working correct as I can execute it in query window and it returns

    @JobStatus
  ------------
  1|Idle

However when executing with EF, the param value is NULL

var param = new SqlParameter
{
    ParameterName = "@JobStatus",
    DbType = DbType.String,
    Size = 30,
    Direction = System.Data.ParameterDirection.Output
};

var result = this.etlContext.Database.SqlQuery<string>("EXEC dbo.up_GetJobStatus @JobStatus OUTPUT", param);

I've also tried the ExecuteSqlCommand method but that didn't work either.

Any ideas?


回答1:


  1. Create stored procedure in database

    CREATE PROCEDURE [dbo].myStoredProcName
        @inputParam1 VARCHAR(150),
        @inputParam2 VARCHAR(150),
        @myOutputParamBool BIT OUTPUT,
        @myOutputParamString VARCHAR(100) OUTPUT,
        @myOutputParamInt INT OUTPUT
    AS
    BEGIN
        -- sql here
    END
    
  2. Update entity model from database to include stored procedure as shown here

  3. Call the stored procedure from C# code

    System.Data.Entity.Core.Objects.ObjectParameter myOutputParamBool=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamBool",typeof(bool));
    System.Data.Entity.Core.Objects.ObjectParameter myOutputParamString=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamString",typeof(string));
    System.Data.Entity.Core.Objects.ObjectParameter myOutputParamInt=new System.Data.Entity.Core.Objects.ObjectParameter("myOutputParamInt",typeof(Int32));
    
    using (var context = new SandCryptEntities())
    {
        context.myStoredProcName(inputParam1, inputParam2, myOutputParamBool, myOutputParamString, myOutputParamInt);   
    }
    
    bool myBool = Convert.ToBoolean(myOutputParamBool.Value);
    string myString = Convert.ToString(myOutputParamString.Value);
    int myInt = Convert.ToInt32(myOutputParamInt.Value);
    



回答2:


Here's an actual answer. Apparently there are serious issues with output parameters in entity framework when you're using DbContext/code first. This article has a good discussion and workaround: http://weblogs.asp.net/dwahlin/using-entity-framework-code-first-with-stored-procedures-that-have-output-parameters. Apparently it's supposed to be "fixed" with an update, but I haven't really seen that happen.




回答3:


Please review this link to execute your stored procedure and retrieve the output parameter. You will first need to import the stored procedure into your dbmx.



来源:https://stackoverflow.com/questions/22068027/executing-sql-stored-procedure-with-output-parameter-from-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!