title: Entity Framework Stored Procedure Table Value Parameter
body: <p>I'm trying to call a stored procedure that accepts a table value parameter.  I know that this isn't directly supported in Entity Framework yet but from what I understand you can do it using the <code>ExecuteStoreQuery</code> command off of the <code>ObjectContext</code>.  I have a generic entity framework repository where I have the following <code>ExecuteStoredProcedure</code> method:</p>
  
  <pre><code>public IEnumerable&lt;T&gt; ExecuteStoredProcedure&lt;T&gt;(string procedureName, params object[] parameters)
  {
      StringBuilder command = new StringBuilder();
      command.Append("EXEC ");
      command.Append(procedureName);
      command.Append(" ");
  
      // Add a placeholder for each parameter passed in
      for (int i = 0; i &lt; parameters.Length; i++)
      {
          if (i &gt; 0)
              command.Append(",");
  
          command.Append("{" + i + "}");
      }
  
      return this.context.ExecuteStoreQuery&lt;T&gt;(command.ToString(), parameters);
  }
  </code></pre>
  
  <p>The command string ends up like this:</p>
  
  <pre><code>EXEC someStoredProcedureName {0},{1},{2},{3},{4},{5},{6},{7}
  </code></pre>
  
  <p>I tried to run this method on a stored procedure that accepts a table valued parameter and it breaks.  I read <a href="https://stackoverflow.com/questions/6084061/udt-as-a-parameter-in-ef4-query">here</a> that the parameters needed to be of type <code>SqlParameter</code> and the table valued parameter needs to have the <code>SqlDbType</code> set to <code>Structured</code>.  So I did this and I get an error stating:</p>
  
  <pre><code>The table type parameter p6 must have a valid type name
  </code></pre>
  
  <p>So, I set the SqlParameter.TypeName to the name of the user defined type I created on the database and then when I run the query I get the following truly helpful error:</p>
  
  <pre><code>Incorrect syntax near '0'.
  </code></pre>
  
  <p>I can get the query to run if I revert back to ADO.NET and and execute a data reader but I was hoping to get it to work using the data context.</p>
  
  <p>Is there a way to pass a table value parameter using <code>ExecuteStoreQuery</code>?  Also, I am actually using Entity Framework Code First and casting the <code>DbContext</code> to an <code>ObjectContext</code> to get the <code>ExecuteStoreQuery</code> method available.  Is this necessary or can I do this against the <code>DbContext</code> as well?</p>