title: How to execute stored procedure and get return result in MVC/EF/LINQ
body: <p>Could any one guide me on how to execute a SQL Server stored procedure in ASP.NET MVC / EF<br>
  application and get results back?</p>
  
  <p><strong>SQL Server stored procedure</strong> </p>
  
  <pre><code> CREATE PROCEDURE dbo.StoredProcedure2 AS
       declare @parameter2 int
       SET @parameter2 = 4
  
       RETURN @parameter2 
  </code></pre>
  
  <p><strong>MVC code</strong></p>
  
  <pre><code> private readonly TestDatastoreContext _context = new TestDatastoreContext();
  
   public ViewResult Index(string id)
   {
          ViewData["EnvironmentId"] = id;
  
          using (_context)
          {
              _context.Database.Connection.Open();
              var command = _context.Database.Connection.CreateCommand();
              command.CommandText = "dbo.StoredProcedure2";
              command.CommandType = System.Data.CommandType.StoredProcedure;
              var test = (command.ExecuteScalar());
          }
  
          var bigView = new BigViewModel
          {
              VersionsModel = _context.Versions.ToList(),
              EnvironmentViewModel = _context.Environments.ToList(),
          };
  
          return View(model: bigView);
  }
  </code></pre>