5

I'm attempting to call a stored proc through EF and retrieve a return value from the stored proc. I've used this answer as a guide. Here is my stored proc:

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

--DO STUFF

SET @result = 0
END

Here is how I'm calling it 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 getting this error message:

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

What am I doing wrong?

6
  • Did you do a SELECT in your stored procedure? If so this is your return type. As a hint - you must consume the resultset before you can get the value of the output parameter.
    – Pawel
    Feb 12, 2016 at 0:20
  • @Pawel: The entire stored proc is in the question. I also tried SET @result = 0.
    – mellis481
    Feb 12, 2016 at 0:23
  • OK. --DO STUFF indicated you are doing something in the body and you don't set the value of the output parameter that's why I did not know if this is a complete procedure...
    – Pawel
    Feb 12, 2016 at 0:25
  • @Pawel: Sorry for the confusion. Is what I'm doing now (updated) how you set an output parameter in the SP?
    – mellis481
    Feb 12, 2016 at 0:27
  • I don't remember at the moment but it might be that EF always expects a resultset. Can you add a dummy SELECT 0 to your stored proc and see if it works?
    – Pawel
    Feb 12, 2016 at 0:30

5 Answers 5

4

Try select @result before end of procedure.

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

--DO STUFF

SET @result = 0
Select @result
END

Hope it works.

1
  • The SELECT at the end of the SP after setting @result did it. Thanks!
    – mellis481
    Feb 12, 2016 at 13:26
1

I wanted to return the new id of an inserted row with a stored procedure. So I also made an OUTPUT parameter. In C# with EF6 I got it as following:

using System.Data.Entity.Core.Objects;
...
db = new myEntities1();
ObjectParameter returnId = new ObjectParameter("returnId", DbType.Int64);
db.postLogItem("New item.", returnId);
MessageBox.Show("Logitem added. New returnId: " + (int)returnId.Value);

The name of the variable in the stored procedure needs to match the one with calling the constructor of ObjectParameter.

CREATE PROCEDURE [dbo].[postLogItem]
    @description nvarchar(200),
    @returnId bigint OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO dbo.LogItem 
        (
            time,
            description
        )
        VALUES
        (
            GETDATE(),
            @description
        );

    SET @returnId = SCOPE_IDENTITY();
END
0

Try adding .First(); to the end...

var result = context.Database.SqlQuery<int>("EXEC usp_test @result", status).First(); 

I don't this you need First() here either since you're returning a single value, this would only work with an IEnumerable - but double check.

var wasSuccessful = result == 1;
0
0

Change ParameterDirection.Output to ParameterDirection.ReturnValue

-3

If nothing works then you can try this alternative approach:

  1. Compute the result in PROC and save it in a table in database
  2. Read the data from that table in EF code.

However, if you have a concurrent system that can run many such EF codes in parallel then ofcourse your stored proc needs become more sophisticated to handle concurrency.

Thanks, hope this might help.

1
  • 2
    This is just the type of hacky solution I typically avoid at all costs.
    – mellis481
    Feb 12, 2016 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.