【问题标题】:Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory无法确定类型为“System.Data.SqlClient.SqlClientFactory”的提供程序工厂的提供程序名称
【发布时间】:2019-04-17 16:11:41
【问题描述】:

我有一个带有控制器的 .Net Core WebApi 项目,该控制器调用服务、类库以获取数据。该服务使用实体框架,而不是 EFCore。错误失败,这里:

public IEnumerable<TEntity> GetAll()
{
    return _dbContext.Set<TEntity>().AsNoTracking().ToList();
}

这是错误:

System.NotSupportedException: '无法确定'System.Data.SqlClient.SqlClientFactory' 类型的提供程序工厂的提供程序名称。确保在应用程序配置中安装或注册了 ADO.NET 提供程序。'

我知道这是可以做到的,这是根据本文将 .NetCore 与 EF 结合使用的首选方式:https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-2.2

“在 ASP.NET Core 应用程序中使用 Entity Framework 6 的推荐方法是将 EF6 上下文和模型类放在以完整框架为目标的类库项目中。从 ASP 添加对类库的引用。 NET Core 项目。请参阅带有 EF6 和 ASP.NET Core 项目的示例 Visual Studio 解决方案。"

这是微软提供的示例:https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/data/entity-framework-6/sample

这是我的 appsettings.json 文件:

    {
        "ConnectionStrings": {
            "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Savi-   Development;Trusted_Connection=True;"
        },
        "Logging": {
            "LogLevel": {
                "Default": "Warning"
            }
        },
        "AllowedHosts": "*"
    } 

这是我的 startup.cs:

    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddScoped<ApplicationDbContext>(_ => new ApplicationDbContext(Configuration.GetConnectionString("DefaultConnection")));
            services.AddScoped<IRegionService, RegionService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }

这是我认为正确的 project.cs 文件。它引用了完整的实体框架。

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="EntityFramework" Version="6.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Savi.Services\Savi.Services.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="EntityFramework.SqlServer">
      <HintPath>..\..\Savi.Services\bin\Debug\EntityFramework.SqlServer.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

我没有 project.fragment.json 文件。

【问题讨论】:

  • 你能分享你的 appsettings.json 和 startup.cs 吗?
  • .net core 还不支持实体框架
  • 这个解决方案有两个项目,一个api core另一个net framework类库。

标签: c# entity-framework .net-core


【解决方案1】:

尝试在链接示例中执行类似操作并手动构建配置,而不是通过构造函数注入它。

例如在Startup 类中

public IConfiguration Configuration { get; }

public Startup(IHostingEnvironment env) {
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

public void ConfigureServices(IServiceCollection services) {
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddScoped<ApplicationDbContext>(_ => 
        new ApplicationDbContext(Configuration.GetConnectionString("DefaultConnection")));
    services.AddScoped<IRegionService, RegionService>();

    //...
}

//...rest omitted for brevity

接下来,我建议您通过搜索您在 .Net Core 上下文中陈述的异常来查看我发现的这个答案中的建议

System.NotSupportedException: Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'

总结一下

您未能将核心 .csproj 文件从 &lt;TargetFramework&gt;netcoreapp2.2&lt;/TargetFramework&gt; 更改为 &lt;TargetFramework&gt;net471&lt;/TargetFramework&gt;

导致 Entity Framework 类(在 .NET Framework 类库中)由 Entity Framework 的 DotNet Core 版本而不是 Entity Framework 的 .NET Framework 版本访问。

您的链接文章还指出

Reference full framework and EF6 in the ASP.NET Core project

您的 ASP.NET Core 项目需要引用 .NET 框架和 EF6。 例如,您的 ASP.NET Core 项目的 .csproj 文件将如下所示 类似于以下示例(仅文件的相关部分是 显示)。

<PropertyGroup>
  <TargetFramework>net452</TargetFramework>
  <PreserveCompilationContext>true</PreserveCompilationContext>
  <AssemblyName>MVCCore</AssemblyName>
  <OutputType>Exe</OutputType>
  <PackageId>MVCCore</PackageId>
</PropertyGroup>

创建新项目时,使用 ASP.NET Core Web 应用程序 (.NET Framework) 模板。

【讨论】:

    【解决方案2】:

    我通过以下操作修复了它:

    1. 新建“ASP.NET Core Web 应用程序”并选择“Net 框架”而不是“核心”。选择“Api”。
    2. 使用与以前相同的代码创建了一个新的“RegionsController”。
    3. 添加了对我现有项目的所有引用。
    4. 在我的新“Api”项目中添加了对“EntityFramework”的引用。
    5. 将我当前在解决方案中的所有项目更新为“Framework 4.7.2”。这有 与错误无关,但我想指出它。
    6. 使用 nuget 将“Newtonsoft”添加到“Api”。
    7. 使用与以前相同的代码更新了新的“Startup.cs”。
    8. 向 appsettings.json 添加了连接字符串。

    @Nikosi 指出,这个引用 https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-2.1#reference-full-framework-and-ef6-in-the-aspnet-core-project 专门说“创建新项目时,使用 ASP.NET Core Web Application (.NET Framework) 模板”。这就是答案。

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多