System.NotSupportedException:无法确定类型为'System.Data.SqlClient.SqlClientFactory'的提供程序工厂的提供程序名称。

Mah*_*dev 6 entity-framework asp.net-core

我使用带有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
Run Code Online (Sandbox Code Playgroud)

Dav*_*ter 7

错误Unable to determine the provider name...可能表明.csproj,根据ASP.NET Core和Entity Framework 6入门中的说明未正确设置Core项目的。

就我而言,我正在与Core 2.2项目一起工作,并尝试利用.Net Framework 4.7.1类库中的Entity Framework 6,但未能将Core .csproj文件从更改<TargetFramework>netcoreapp2.2</TargetFramework><TargetFramework>net471</TargetFramework>

当我最终更改目标框架时,出现的下一个错误是Microsoft.AspNetCore.All 2.2.1 is not compatible with net471 (.NETFramework,Version=v4.7.1)。我删除了NuGet Microsoft.AspNetCore.All程序包,然后创建了许多缺少的参考构建错误。

要解决构建错误,我必须用谷歌搜索缺少的类型以找到程序集名称,添加特定的NuGet程序包,进行重建,然后继续重复此过程,直到解决了我所有的构建错误。

每个项目都是不同的,但是在删除后,我必须添加以下软件包来解决我的构建问题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" />
Run Code Online (Sandbox Code Playgroud)

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


总而言之,System.NotSupportedException发生这种情况是因为所生成的Entity Framework类(在.NET Framework 471类库中)是由Entity Framework的DotNet Core版本而不是Entity Framework的.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”