我正在使用带有“代码优先”方法的Entity Framework 5。我需要从存储过程中读取返回值;我已经在读取输出参数并发送输入参数,但是我不知道如何读取返回值。
可能吗?
这是我用来调用存储过程的代码:
var outParam = new SqlParameter(); outParam.ParameterName = "@StatusLog"; outParam.SqlDbType = SqlDbType.NVarChar; outParam.Size = 4000; outParam.Direction = ParameterDirection.Output; var code = new SqlParameter(); code.ParameterName = "@Code"; code.Direction = ParameterDirection.Input; code.SqlDbType = SqlDbType.VarChar; code.Size = 20; code.Value = "123"; var data = _context.Database.SqlQuery<Item>("exec spItemData @Code, @StatusLog OUT", code, outParam); var result = data.FirstOrDefault();
我找到了!我可以使用必须以这种方式使用的输出参数读取返回值:
// 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);