Stored Procedure returns incorrect scalar value of -1, instead of return value

后端 未结 4 683
日久生厌
日久生厌 2021-01-02 20:36

I am trying to return a scalar value from a stored procedure. I actually want to return the ID of a newly created record, but I have simplified my problem down to a stored p

4条回答
  •  情话喂你
    2021-01-02 21:26

    Thank you, DavidG for the article. It got me started down the right path. So what I did to solve this was change my Stored Procedure to return an ObjectResult instead of an int. Then I did a SingleOrDefault() on the results of the Stored Procedure call, which yielded my int return value. Like this:

    Stored Proc:

    -- RETURN @return does not work. Can't return a scalar value  
    SELECT @return  -- This returns a result set with a single object that contains an int.
    

    Then, my generated code looks like above but instead of returning an int it returns an ObjectResult

    and I read the results like this:

    var id = myDbContext.My_Return_Int(123).SingleOrDefault();
    

提交回复
热议问题