Entity Framework - Stored Procedure Return Value

Get return value from stored procedure

I found it!
I can read the return value with an output parameter that has to be used in this way:

// define a new output parameter
var returnCode = new SqlParameter();
returnCode.ParameterName = "@ReturnCode";
returnCode.SqlDbType = SqlDbType.Int;
returnCode.Direction = ParameterDirection.Output;

// assign the return code to the new output parameter and pass it to the sp
var data = _context.Database.SqlQuery<Item>("exec @ReturnCode = spItemData @Code, @StatusLog OUT", returnCode, code, outParam);

Entity Framework - stored procedure return value

No. Entity Framework doesn't have rich stored procedure support because its an ORM, not a SQL replacement.

As you have already discovered, if you need to use less common (more advanced?) features of stored procedures, you'll have to use good old fashioned ADO.NET.

How to return a value from a stored procedure to EF

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.

Entity Framework returns the wrong value from stored procedure

@UserName varchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
Select count(LoginID) UserCount
from Personnel
where LoginID = @UserName And Administrator = 'Y'

END

Entity code:

private void ValidateUser(string user)
{
using (ComplaintsEntities db = new ComplaintsEntities())
{
var t = db.AAGetAdminStatus(user).First().UserCount;
}
}

How to fetch return value from stored procedure in entity framework?

In your code, create a bool that will hold the result of your storedproc and will return true if the result of your storedproc is 1. Otherwise false.

bool result = _entites.StoredProc(parameters);
return (result == 1) ? true: false;

But in your stored proc
You should create a procedure that will return a value like this

CREATE PROCEDURE [dbo].[StoredProc]
@parameter
AS
BEGIN
DECLARE @result int
DECLARE @param nvarchar(255)
SET @param = @parameter

INSERT INTO (field1) VALUES (@param)

IF @@ERROR = 0
SET @result = 1
ELSE
SET @result = 0

RETURN @result

@@Error returns 0 if the T-SQl statement encountered no errors. You can use that to set the result to 1 or 0. Refer to this link for more info for @@error.

how can i check return value of stored procedure in entity framework

Database First

First, you have to add your stored procedure to the .edmx file.
If you have a context variable _DBContext and the stored procedure is called LandMarkInOutReport, you can execute it like this:

LandMarkInOutReport_Result returnValue = _DBContext.LandMarkInOutReport(report.ReportParameters.StartDate, report.ReportParameters.EndDate, Convert.ToInt64(paramArr1[3]), Convert.ToInt32(paramArr1[9]), Convert.ToInt32(paramArr1[11]), paramArr1[5], paramArr1[7]).FirstOrDefault();

The stored procedure call without .FirstOrDefault() isn't executed on the database.

Now you can use the returnValue to call the correct variable eg.: returnValue.ReturnVariableName.

Code First

You can call the stored procedure with .SqlQuery<>:

int returnValue = _DBContext.SqlQuery<int>("LandMarkInOutReport @StartDate, @EndDate, @param3, @param4, @param5, @param6, @param7", 
new SqlParameter("StartDate", report.ReportParameters.StartDate),
new SqlParameter("EndDate", report.ReportParameters.EndDate),
new SqlParameter("param3", Convert.ToInt64(paramArr1[3])),
new SqlParameter("param4", Convert.ToInt32(paramArr1[9])),
new SqlParameter("param5", Convert.ToInt32(paramArr1[11])),
new SqlParameter("param6", paramArr1[5]),
new SqlParameter("param7", paramArr1[7])).FirstOrDefault();


Related Topics



Leave a reply



Submit