The stored procedure returns an invalid scalar value of -1 instead of the return value

I am trying to return a scalar value from a stored procedure. I really want to return the ID of the newly created record, but I simplified my problem to a stored procedure that accepts intand tries to return the same one int. This always returns -1. Thank you so much for your help.

Web API Manager Call

var idtest = dbconn.my_return_int(123);

Stored procedure:

ALTER PROCEDURE [dbo].[my_return_int]  
    @ID int  
AS  
BEGIN  
    SET NOCOUNT ON;  
    DECLARE @return as int

    SET @return = -999

RETURN @return
END   

The generated stored procedure call Context

public virtual int my_return_int(Nullable<int> iD)  
{  
    var iDParameter = iD.HasValue ?
        new ObjectParameter("ID", iD) :
        new ObjectParameter("ID", typeof(int));

    return (IObjectContextAdapter)this).ObjectContext.ExecuteFunction("my_return_int", iDParameter);  
}  
+5
source share
4 answers

When you execute the ObjectContext.ExecuteFunctionresult will be:

MSDN: , ; ,

.. , , . , SET NOCOUNT ON; , -1.

, :

  1. , , , .. RETURN @return SELECT @return AS alias. , "AS alias". alias , .
  2. , int32 int32. , .

, , .

+6

, , , , , EF :

RETURN(0)

:

BEGIN
    SELECT(0)
    RETURN(0)
END

, IF,
מ : EF- (.edmx) → → → ( ) → Scala → Int32

:

ObjectResult<Nullable<int>>

, :

int? response = 0;
response = myEntity.myStoredProcedure(var1, var2).FirstOrDefault();

:
1.
Model browser
2.
Function imports
3.
Scalars

EF (.edmx) " ...", , .

+4

, DavidG . . ObjectResult<int?> , ObjectResult<int?> int. SingleOrDefault() , int. :

:

-- 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 intit returnsObjectResult<int?>

and I read the results like this:

var id = myDbContext.My_Return_Int(123).SingleOrDefault();
+1
source

I just implemented another option in my project. I am giving another output parameter to @returnurn.

0
source

Source: https://habr.com/ru/post/1544823/


All Articles