【问题标题】:System.NotSupportedException: Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'System.NotSupportedException:无法确定“System.Data.SqlClient.SqlClientFactory”类型的提供程序工厂的提供程序名称
【发布时间】:2018-09-05 12:23:00
【问题描述】:

我正在使用带有 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

【问题讨论】:

标签: entity-framework asp.net-core


【解决方案1】:

错误Unable to determine the provider name... 可能表明核心项目的.csproj 未按照Get Started with ASP.NET Core and Entity Framework 6 上的说明正确设置。

就我而言,我正在使用 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" />

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


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

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

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

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

【讨论】:

  • 谢谢!我试图使用相同的指南,但肯定有一些技巧。你的回答对我帮助很大。
【解决方案2】:

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

您可以尝试NHibernate 用于 .Net Core 项目。

【讨论】:

    【解决方案3】:

    更新两个项目的EntityFramework Package

    例如6.4.4版本

    【讨论】:

      猜你喜欢
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      相关资源
      最近更新 更多