title: Can I test the construction of a LINQ query without actually executing it
body: <p>I have heard that when testing EF, you need to use integration testing against a live DB because of the differences between the LINQ to Objects and LINQ to Entities providers.</p>
  <p>Why can't we unit test the <em>construction</em> of the query without actually <em>executing</em> it?  I would have thought you could just attach your IQueryable to the LINQ to Entities provider <em>somehow</em> and confirm the SQL is generated properly (using something like ToTraceString which does not execute the actual query).</p>
  <p>I imagine code along these lines (this query would run fine in L2O but not in L2E):</p>
  <pre><code>    &lt;Test()&gt; _
      Public Sub Query_Should_Build_Against_L2E()
          Dim testQuery = From d In myDb
                          Where d.Status = CType(Status.Ready, Integer)
  
          testQuery.SetQueryProvider(&quot;L2E&quot;)
  
          Assert.DoesNotThrow(testQuery.ToTraceString())
      End Sub
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>I tried implement GertArnold's solution as follows:</p>
  <pre><code>Dim context As New Context(&quot;Data Source=fakedbserver;Initial Catalog=fakedb;Persist Security Info=True;&quot;)
  Dim result = context.myTable.Where(Function(d) d.Status=True)
  </code></pre>
  <p>That throws a <code>ProviderIncompatibleException</code> with the message &quot;An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.&quot;  Here's the full exception chain:</p>
  <blockquote>
  <p>System.Data.ProviderIncompatibleException : An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.</p>
  <p>System.Data.ProviderIncompatibleException : The provider did not return a ProviderManifestToken string.</p>
  <p>System.InvalidOperationException : This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.</p>
  <p>System.Data.SqlClient.SqlException : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)</p>
  </blockquote>