System.NotSupportedException:无法确定类型为'System.Data.SqlClient.SqlClientFactory'的提供程序工厂的提供程序名称. [英] System.NotSupportedException: Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'

查看:97
本文介绍了System.NotSupportedException:无法确定类型为'System.Data.SqlClient.SqlClientFactory'的提供程序工厂的提供程序名称.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Asp.net核心的EF6,通过dbcontext调用db时出现错误:

System.NotSupportedException: Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
   at System.Data.Entity.Utilities.DbProviderFactoryExtensions.GetProviderInvariantName(DbProviderFactory factory)
   at System.Data.Entity.Infrastructure.DependencyResolution.DefaultInvariantNameResolver.GetService(Type type, Object key)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
   at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key)
   at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)
   at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInvariantName(DbConnection connection)
   at System.Data.Entity.Internal.InternalConnection.get_ProviderName()
   at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at CoreWithEF.controllers.SampleController.GetStudentDetails() in C:\Users\biradm1\Documents\Visual Studio 2017\Projects\CoreWithEF\CoreWithEF\controllers\SampleController.cs:line 27
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()}  System.NotSupportedException

解决方案

根据

在解决了构建问题并成功构建了项目之后,我的DotNet Core 2.2 API能够使用Entity Framework 6从类库项目中返回数据.


总而言之,发生System.NotSupportedException的原因是,生成的Entity Framework类(在.NET Framework 471类库中)是由Entity Framework的DotNet Core版本而不是Entity的.NET Framework 4.7.1版本访问的框架.

在进行上面列出的更改之前,在即时调试"窗口中评估System.Data.Common.DbProviderFactories.GetFactoryClasses().Rows会显示一个空的提供程序集合.进行更改后,工厂类包括Odbc,OleDb,Oracle,SqlServer提供程序.

更具体地说,如果单独评估行,则应该看到提供程序来自完整的.NET Framework.

例如:System.Data.Common.DbProviderFactories.GetFactoryClasses().Rows[3].ItemArray(对我来说)表明它来自"System.Data.SqlClient.SqlClientFactory, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.

I am using EF6 with Asp.net core and I am getting error when I call to db through dbcontext :

System.NotSupportedException: Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
   at System.Data.Entity.Utilities.DbProviderFactoryExtensions.GetProviderInvariantName(DbProviderFactory factory)
   at System.Data.Entity.Infrastructure.DependencyResolution.DefaultInvariantNameResolver.GetService(Type type, Object key)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
   at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key)
   at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)
   at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInvariantName(DbConnection connection)
   at System.Data.Entity.Internal.InternalConnection.get_ProviderName()
   at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at CoreWithEF.controllers.SampleController.GetStudentDetails() in C:\Users\biradm1\Documents\Visual Studio 2017\Projects\CoreWithEF\CoreWithEF\controllers\SampleController.cs:line 27
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()}  System.NotSupportedException

解决方案

The error Unable to determine the provider name... can indicate the .csproj for the Core project is not set correctly per the instructions on Get Started with ASP.NET Core and Entity Framework 6.

In my case I was working with Core 2.2 project and trying to leverage Entity Framework 6 from .Net Framework 4.7.1 class library, but failed to change the Core .csproj file from <TargetFramework>netcoreapp2.2</TargetFramework> to <TargetFramework>net471</TargetFramework>.

When I finally made the target framework change, the next error to appear was Microsoft.AspNetCore.All 2.2.1 is not compatible with net471 (.NETFramework,Version=v4.7.1). I removed the NuGet Microsoft.AspNetCore.All package, which then created a lot of missing reference build errors.

To resolve the build errors I had to google the missing type to find the assembly name, add that specific NuGet package, rebuild, then keep repeating the process until all of my build errors were resolved.

Every project is different, but here were the packages I had to add to fix my build issues after removing Microsoft.AspNetCore.All:

<PackageReference Include="EntityFramework" Version="6.2.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />

After build issues were resolved and the project built successfully, my DotNet Core 2.2 API was able to return data using Entity Framework 6 from the class library project.


To summarize, the System.NotSupportedException occurred because the generated Entity Framework classes (in the .NET Framework 471 class library) were being accessed by the DotNet Core version of Entity Framework and not the .NET Framework 4.7.1 version of Entity Framework.

Before making the changes listed above, evaluating System.Data.Common.DbProviderFactories.GetFactoryClasses().Rows in the Immediate debug window showed an empty collection of providers. After making the changes, the Factory Classes included the Odbc, OleDb, Oracle, SqlServer providers.

More specifically if you evaluate the rows individually, you should see the providers are from the Full .NET Framework.

For example: System.Data.Common.DbProviderFactories.GetFactoryClasses().Rows[3].ItemArray (for me) showed it came from "System.Data.SqlClient.SqlClientFactory, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".

这篇关于System.NotSupportedException:无法确定类型为'System.Data.SqlClient.SqlClientFactory'的提供程序工厂的提供程序名称.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆