title: Entity Framework: how do I run a stored procedure and return a value?
body: <p>Is it possible to execute a stored procedure using the Entity Framework, passing in variables and returning data along with a return value?</p>
  
  <p>Please do not leave any code about function mappings with Entity Framework.</p>
  
  <p>I have tried this and I keep getting the same damn error:</p>
  
  <blockquote>
    <p>Procedure or function 'usp_BusinessUser_Search' expects parameter
    '@TotalRecords', which was not supplied.</p>
  </blockquote>
  
  <p>Here's my code:</p>
  
  <pre><code>SqlParameter Business = new SqlParameter("Business", Search.Term);
  SqlParameter Location = new SqlParameter("Location", Search.Location);
  SqlParameter PageNumber = new SqlParameter("PageNumber", Search.CurrentPage);
  SqlParameter RecordsPerPage = new SqlParameter("RecordsPerPage", Search.RecordsPerPage);
  SqlParameter TotalRecords = new SqlParameter("TotalRecords", 0);
  
  SqlParameter parm = new SqlParameter()
  {
      ParameterName = "@TotalRecords",
      Value = 0,
      SqlDbType = SqlDbType.Int,
      Direction = System.Data.ParameterDirection.Output
  };
  
  var List = db.ExecuteStoreQuery&lt;ENT_SearchBusinessResult&gt;("exec usp_BusinessUser_Search",Business,Location,PageNumber,RecordsPerPage,parm); 
  </code></pre>
  
  <p>Does anyone have any idea what is causing this?</p>
  
  <p>Thanks</p>
  
  <p>EDIT:</p>
  
  <p>Stored procedure code:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[usp_BusinessUser_Search] (   @Business nVarChar(255) = NULL
                                                      , @Location nVarChar(255) = NULL
                                                      , @PageNumber Int = 1
                                                      , @RecordsPerPage Int = 10
                                                      , @TotalRecords Int OUTPUT)
  AS 
  BEGIN
      SET NOCOUNT ON;
  
      DECLARE @SQL NVARCHAR(MAX)
              , @CacheTable SYSNAME
              , @TotalRows Int
              , @Category VarChar(255)
              , @BusinessCategory Int 
              , @TownCounty VarChar(50)
              , @PcodeTownCounty VarChar(50)
  
      INSERT  Voucher_SearchHistory (Keyword, Location)
      SELECT  NULLIF(@Business,''), NULLIF(@Location,'')
  </code></pre>