dotnet / ef6

This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore.
https://docs.microsoft.com/ef/ef6
MIT License
1.41k stars 538 forks source link

Cannot use EF6 with Microsoft.EntityFramework.SqlServer and Microsoft.Data.SqlClient #2251

Closed levimatheri closed 1 month ago

levimatheri commented 1 month ago

I installed the preview version of Microsoft.EntityFramework.SqlServer (v6.5.0-preview2-24180-01) and I can't get it to work. I keep getting this error message: _[InvalidCastException: Unable to cast object of type 'Microsoft.Data.SqlClient.SqlConnection' to type 'System.Data.SqlClient.SqlConnection'.].

This fails when I go to access DbContext methods.

var applications = await dbContext
                .AsQueryable<Application>()
                .Include(x => x.ConnectionInfoes.Select(info => info.EnvironmentType))
                .Include(x => x.ApplicationRedirects.Select(redirect => redirect.EnvironmentType))
                .ToListAsync();

I have configured this in Web.config:

<connectionStrings>
    <add name="ConfigurationEntities" connectionString="metadata=res://*/ConfigurationEntities.csdl|res://*/ConfigurationEntities.ssdl|res://*/ConfigurationEntities.msl;provider=Microsoft.Data.SqlClient;provider connection string=&quot;data source=xxxxxxx;initial catalog=xxxxxxx;Authentication=Active Directory Default;Connection Timeout=60;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
    <providers>
        <provider invariantName="Microsoft.Data.SqlClient" type="System.Data.Entity.SqlServer.MicrosoftSqlProviderServices, Microsoft.EntityFramework.SqlServer" />
    </providers>
</entityFramework>
<system.data>
    <DbProviderFactories>
        <add name="SqlClient Data Provider" invariant="Microsoft.Data.SqlClient" description=".NET Framework Data Provider for SqlServer" type="Microsoft.Data.SqlClient.SqlClientFactory, Microsoft.Data.SqlClient" />
    </DbProviderFactories>
</system.data>

I've looked everywhere and I'm not referencing 'System.Data.SqlClient'. Please assist.

Here's the stack trace:

[InvalidCastException: Unable to cast object of type 'Microsoft.Data.SqlClient.SqlConnection' to type 'System.Data.SqlClient.SqlConnection'.]
   at System.Data.SqlClient.SqlCommand.set_DbConnection(DbConnection value)
   at System.Data.Entity.Core.Common.Utils.CommandHelper.SetStoreProviderCommandState(EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.PrepareEntityCommandBeforeExecution(EntityCommand entityCommand)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.<ExecuteStoreCommandsAsync>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.<ExecuteAsync>d__10`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__156`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<ExecuteAsyncImplementation>d__6`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<GetResultsAsync>d__43.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Internal.LazyAsyncEnumerator`1.<FirstMoveNextAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.<ForEachAsync>d__2`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at TestWebApp.Controllers.TestController.<Get>d__2.MoveNext() in C:\Users\muriukilm\source\repos\TestWebApp\TestWebApp\Controllers\TestController.cs:line 30

EF version: 6.5.0-preview2-24180-01 Database provider: SQL Server (Azure) Target framework: .NET Framework 4.7.2 Operating system: Windows 11 IDE: Visual Studio 2022 17.10.0

ErikEJ commented 1 month ago

You are not sharing any details about your implementation, but could this be a duplicate of https://github.com/dotnet/ef6/issues/2250 ?

Otherwise share a runnable console app that demonstrates the issue, and I will have a closer look.

ErikEJ commented 1 month ago

@levimatheri Did you update you configuration in code as described in the readme (code changes are required)

levimatheri commented 1 month ago

@ErikEJ what helped was the suggestion you linked to, to remap System.Data.SqlClient to Microsoft.Data.SqlClient using DbConfiguration

ErikEJ commented 1 month ago

@levimatheri Seems like an exact duplicate

@AndriySvyryd Feel free to close this! Wonder if we should add something to the readme regarding this?

AndriySvyryd commented 1 month ago

@ErikEJ Since we've had 2 people hit this already, I think that adding it to the readme would be helpful. I am also considering duplicating the readme as a separate page in Docs under What's new, as some users look there first.

ErikEJ commented 1 month ago

@AndriySvyryd I will try to think of something to add to the readme.