title: How to execute stored procedure and get output from the stored procedure using Entity Framework
body: <p>I have been trying to insert a record with using EF6 by way of calling a stored procedure and trying to get the output from the stored procedure which is the newly created ID but keep running into brick walls.</p>
  
  <p>I have a stored procedure that has an OUTPUT of ContactID</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[usp_InsertContact]
      @ContactID int output,
      @Name nvarchar(50)
  AS
  insert into [Contact]
      (Name) values (@Name)
      set @ContactID = @@IDENTITY
  </code></pre>
  
  <p>I am calling the stored procedure and passing the values like this...</p>
  
  <pre><code>return Convert.ToInt32(AWJE.Database.SqlQuery&lt;int&gt;
     ("usp_InsertContact @Name", contact.Name));
  </code></pre>
  
  <p>That didn't work either I got the following error...(and I had posted this earlier and answered it myself, and it didn't work).</p>
  
  <blockquote>
    <p>System.Data.Entity.Infrastructure.DbRawSqlQuery`1[System.Int32]' to type 'System.IConvertible Error</p>
  </blockquote>
  
  <p>Then I went and tried this...</p>
  
  <pre><code>return AWJE.Database.ExecuteSqlCommand
      ("exec usp_InsertContact @Name", contact.Name);
  </code></pre>
  
  <p>When this gets called I get the following error...</p>
  
  <blockquote>
    <p>Additional information: Must declare the scalar variable "@Name".</p>
  </blockquote>
  
  <p>As you can see the variable @Name is in the stored procedure and that I am passing the variable @Name.</p>
  
  <p>So then I tried this</p>
  
  <pre><code>return AWJE.Database.ExecuteSqlCommand
     ("exec usp_InsertContact @ContactID OUTPUT, @Name", contact.Name);
  </code></pre>
  
  <p>And that gives this error...</p>
  
  <blockquote>
    <p>Additional information: Must declare the scalar variable "@ContactID".</p>
  </blockquote>
  
  <p>So I am kind of lost with how to do this, I know how to do this strictly using ADO.NET.
  I have looked around for an answer for this but not understanding examples I have seen because the examples I have seen are being mixed with SqlParameter.Add etc...</p>