ErikEJ / EntityFramework6PowerTools

This is the codebase for Entity Framework 6 Power Tools Community Edition, and modern EF 6 providers for SQL Server and SQL Server Compact
Other
183 stars 27 forks source link

'Unable to cast object of type 'Microsoft.Data.SqlClient.SqlConnection' to type 'System.Data.SqlClient.SqlConnection'.' #127

Closed jfrank14 closed 1 year ago

jfrank14 commented 1 year ago

I'm using EF6 and .NET Framework 4.8, and I'm trying to upgrade to use the new provider, for the usual reasons.

When I make my first EF query, I get the above error, even though I've followed the configuration instructions completely, as far as I can see.

jfrank14 commented 1 year ago

The stack trace, if useful, is:

   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.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass41_0.<GetResults>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__31_0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at {the place in the stack where I'm trying to make the EF call}
ErikEJ commented 1 year ago

Something somewhere is still configured to use the old ado.net provider.

jfrank14 commented 1 year ago

Any idea how I can find that something?

ErikEJ commented 1 year ago

Carefully read the readme? Or share a repro.

And inspect your dependencies.

ErikEJ commented 1 year ago

Any news?

jfrank14 commented 1 year ago

I think I found the problem in the EDMX file itself <Schema Namespace="Data" Provider="Microsoft.Data.SqlClient" ProviderManifestToken="2012" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">

It took me a while to think to look in a file that I never use directly.

ErikEJ commented 1 year ago

As documented 😉