I heard that when testing EF you need to use integration testing with a live database due to the differences between LINQ to Objects and LINQ to Entities providers.
Why can't we unit test build a query without actually executing it? I would think that you attach your IQueryable to the LINQ to Entities provider as soon as possible and make sure that the SQL is generated correctly (using something like ToTraceString, which does not fulfill the actual query).
I present the code in these lines (this request will work fine in L2O, but not in L2E):
<Test()> _ Public Sub Query_Should_Build_Against_L2E() Dim testQuery = From d In myDb Where d.Status = CType(Status.Ready, Integer) testQuery.SetQueryProvider("L2E") Assert.DoesNotThrow(testQuery.ToTraceString()) End Sub
EDIT
I tried to implement the GertArnold solution as follows:
Dim context As New Context("Data Source=fakedbserver;Initial Catalog=fakedb;Persist Security Info=True;") Dim result = context.myTable.Where(Function(d) d.Status=True)
This causes a ProviderIncompatibleException message with the message "An error occurred while retrieving vendor information from the database. It could be caused by the Entity Framework using the wrong connection string. Check the internal exceptions for more details and make sure the connection string is correct." Here's the full chain of exceptions:
System.Data.ProviderIncompatibleException: An error occurred while retrieving provider information from the database. This may be caused by the Entity Framework using the wrong connection string. Check the internal exceptions and verify that the connection string is correct.
System.Data.ProviderIncompatibleException: the provider did not return the ProviderManifestToken string.
System.InvalidOperationException: This operation requires a connection to the master database. The connection to the master database could not be created because the original database connection was open and the credentials were removed from the connection string. Install unopened connection.
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 available. Verify the instance name is correct and configure SQL Server to connect remotely. (provider: Named Pipes provider, error: 40 - Could not open SQL Server connection)