简体   繁体   English

如何在实体框架中检查存储过程的返回值

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

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? 因此,请指导我如何获取返回值,这是在实体框架中调用存储过程的正确方法吗?

Database First 数据库优先

First, you have to add your stored procedure to the .edmx file. 首先,您必须将存储过程添加到.edmx文件。 If you have a context variable _DBContext and the stored procedure is called LandMarkInOutReport , you can execute it like this: 如果您有一个上下文变量_DBContext ,并且存储过程称为LandMarkInOutReport ,则可以这样执行它:

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. 没有在数据库上不执行没有.FirstOrDefault()的存储过程调用。

Now you can use the returnValue to call the correct variable eg.: returnValue.ReturnVariableName . 现在,您可以使用returnValue调用正确的变量,例如: returnValue.ReturnVariableName

Code First 代码优先

You can call the stored procedure with .SqlQuery<> : 您可以使用.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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM