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

c# entity-framework-6 stored-procedures

Question

I have a stored procedure call but i want to get the return value of the stored procedure. Return type is integer. Following the code where I am calling the stored procedure

 else if(ReportName=="LandMarkInOutReport")
        {
          _DBContext.LandMarkInOutReport(report.ReportParameters.StartDate, report.ReportParameters.EndDate, Convert.ToInt64(paramArr1[3]), Convert.ToInt32(paramArr1[9]), Convert.ToInt32(paramArr1[11]), paramArr1[5], paramArr1[7]);     
        }

So please guide me how can I get the return value and also that is it the correct way to call stored procedure in entity framework?

1
1
6/2/2015 6:53:09 AM

Accepted Answer

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();
1
6/2/2015 6:33:38 AM


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