系统不支持异常:无法确定类型为“System.Data.SqlClient.SqlClientFactory”的提供程序工厂的提供程序名称。

13

我正在使用EF6与Asp.net core,当我通过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

当应用程序配置文件中未定义连接字符串或尚未安装EntityFramework时,我遇到了该错误。 - JPocoata
你是否遵循ASP.NET Core和Entity Framework 6入门?请分享详细的步骤以重现您的问题。 - Edward
这是我的代码:https://github.com/maddy027/IssueWithEF6WithCore.git。我有什么遗漏吗? - Mahadev
3个回答

12
错误 无法确定提供程序名称... 可能表示按照 开始使用 ASP.NET Core 和 Entity Framework 6 的说明,未正确设置核心项目的.csproj。在我的情况下,我正在使用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与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" />

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


总之,发生了System.NotSupportedException是因为DotNet Core版本的Entity Framework访问了生成的Entity Framework类(在.NET Framework 471类库中),而不是.NET Framework 4.7.1版本的Entity Framework。在进行上述更改之前,在Immediate调试窗口中评估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”

1
谢谢!我一直在尝试使用同样的指南,但肯定有一些技巧。你的答案帮了我很多。 - Seano666

3

更新项目中的EntityFramework包

例如,更新到6.4.4版本。


1

如先前答案所述:Entity Framework 6 不支持 .NET Core。如果需要跨平台功能,您需要升级到 Entity Framework Core

对于 .Net Core 项目,您可以尝试使用 NHibernate


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接