title: How to create a temporary table and use it in the same connection with Entity Framework?
body: <p>I am trying to execute a three raw queries with Entity Framework.</p>
  
  <p>The first query will basically create a temporary table. The second query will add an index on the temporary table. Finally, the second query will join to the temporary table to other table to get a final dataset.</p>
  
  <p>But every time I run my code, get the following error</p>
  
  <blockquote>
    <p>invalid <code>#allRecords</code> object.</p>
  </blockquote>
  
  <p>Here is what I have done</p>
  
  <pre><code>using (BaseContextdb = new BaseContext())
  {
      using (var dbContextTransaction = db.Database.BeginTransaction())
      {
          try
          {
              db.Database.ExecuteSqlCommand("SELECT col1, col2, col3 " +
                                            "INTO #allRecords " +
                                            "FROM someTable " +
                                            "WHERE col5 = 'blab' " +
                                            "CREATE INDEX d ON #allRecords(col1, col2); ");
  
              var results = db.Database.SqlQuery&lt;ResuleModel&gt;(this.GetQuery()).ToList();
  
              db.SaveChanges();
  
              dbContextTransaction.Commit();
          }
          catch (Exception)
          {
              dbContextTransaction.Rollback();
          }
      }
  }
  </code></pre>
  
  <p>how can I correctly create temporary table with Entity Framework?</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>Here is the query that is returned by <code>this.GetQuery()</code></p>
  
  <pre><code>SELECT b.*, c.* 
  FROM b
  INNER JOIN #allRecords AS a ON a.col1 = v.col1 AND a.col2 = b.col2
  INNER JOIN c ON c.Id= b.Id
  ...
  ...
  ...
  </code></pre>