How to return a value from a stored procedure to EF

c# entity-framework sql sql-server

Question

I'm trying to use EF to call a stored procedure and get the return value from the procedure. I followed the advice in the this answer. This is my stored procedure:

CREATE PROCEDURE [dbo].[usp_test]
(
    @result int OUT
)
AS
BEGIN

--DO STUFF

SET @result = 0
END

I'm calling it like this from EF:

var status = new SqlParameter
{
    ParameterName = "result",
    DbType = DbType.Int32,
    Direction = ParameterDirection.Output 
};
var result = context.Database.SqlQuery<int>("EXEC usp_test @result", status);
var wasSuccessful = result.First() == 1;
if (!wasSuccessful)
{
    //DO STUFF
}

I'm experiencing the following error:

The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types

Why am I misusing this?

1
5
5/23/2017 12:26:07 PM

Accepted Answer

ZZZ_tmp
4
2/12/2016 11:47:29 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow