title: Entity Framework: how to execute stored procedure with multiple parameters passed as an array
body: <p>I have two functions for running stored procedures, one returns a collection, and one just simply executes a stored procedure.</p>
  <p>The first one that returns a collection works perfectly and has been tested with plenty of parameters in the array:</p>
  <pre><code>public ICollection&lt;TObject&gt; RunStoredProcedure_ReturnCollection_Generic(string spName, List&lt;string&gt; paramFields, List&lt;string&gt; paramFieldValues)
  {
      string queryStr = &quot;exec &quot; + spName;
      List&lt;SqlParameter&gt; pars = new List&lt;SqlParameter&gt;();
  
      if (paramFields != null)
      {
          for (int i=0; i &lt;= (paramFields.Count - 1); i++)
          {
              SqlParameter par = new SqlParameter();
              par.ParameterName = paramFields[i];
              par.Value = paramFieldValues[i];
              pars.Add(par);
  
              if (queryStr.Contains(&quot;@&quot;))
              { 
                  queryStr = queryStr + &quot;,&quot;; 
              }
  
              queryStr = queryStr + &quot; @&quot; + par.ParameterName;   
          }
  
          return _context.Database.SqlQuery&lt;TObject&gt;(queryStr, pars.ToArray()).ToList();
      }
      else
      {
          return _context.Database.SqlQuery&lt;TObject&gt;(&quot;exec &quot; + spName).ToList();
      }
  }
  </code></pre>
  <p>My second function, which simply should execute and has nothing to return does not work and I can't understand why:</p>
  <pre><code>public void ExecuteProcedure_Generic(string spName, List&lt;string&gt; paramFields, List&lt;string&gt; paramFieldValues)
  {
      string queryStr = &quot;exec &quot; + spName;
      List&lt;SqlParameter&gt; pars = new List&lt;SqlParameter&gt;();
  
      if (paramFields != null)
      {
          for (int i = 0; i &lt;= (paramFields.Count - 1); i++)
          {
              SqlParameter par = new SqlParameter();
              par.ParameterName = paramFields[i];
              par.Value = paramFieldValues[i];
              pars.Add(par);
  
              if (queryStr.Contains(&quot;@&quot;))
              { 
                  queryStr = queryStr + &quot;,&quot;; 
              }
  
              queryStr = queryStr + &quot; @&quot; + par.ParameterName;
          }
  
          _context.Database.ExecuteSqlCommand(queryStr, pars.ToArray());
      }
      else
      {
          _context.Database.ExecuteSqlCommand(spName);
      }
  }
  </code></pre>
  <p>The error that I get on the <code>ExecuteSqlCommand</code> line is:</p>
  <blockquote>
  <p>An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.SqlServer.dll but was not handled in user code. Additional information: Procedure or function 'TidyTempTableByUserID' expects parameter '@UserID', which was not supplied</p>
  </blockquote>
  <p>I have trapped the code and the <code>queryStr</code> looks to be an identical format as in my previous working function, and of course the <code>pars</code> array is populated with both parameters and their values. <code>queryStr</code> at the time of execution reads:</p>
  <pre><code>exec Orders_OrderLineStaging_Append @UserID, @MealPlanID
  </code></pre>
  <p>Can anyone see what I'm doing wrong? Many thanks in advance.</p>