Home > Blockchain >  How can I get output value from the stored procedure in C# using Entity Framework
How can I get output value from the stored procedure in C# using Entity Framework

Time:05-15

I have connected a stored procedure to a C# program using Entity Framework with a .edmx model. I am trying to get an integer output value to a variable from the stored procedure which was connected to entity. How can I get the output result from the stored procedure to the local variable?

Here is the code in C#

_db = new DbEntities();
int id = 0;
var fleetid = _db.GetNextFleetId(id);

SQL Server stored procedure:

PROCEDURE dbo.GetNextFleetId 
    @NewId bigint OUTPUT
AS 
BEGIN
    SELECT @NewId = NEXT VALUE FOR dbo.seqFleets;
    RETURN;
END

CodePudding user response:

If we add stored procedure to edmx model from the database.

we can try to use ObjectParameter to get output value from stored procedure.

ObjectParameter outPutId = new ObjectParameter("NewId", typeof(long));
_db.GetNextFleetId(outPutId);

//output value
Convert.ToInt64(outPutId.Value);
  • Related