在安装了.NET Core 2.1的SQL客户端后,DbProviderFactories.GetFactoryClasses没有返回任何结果。

12
我现在正在将一个库迁移到.NET Core 2.1,因为它支持DbProviderFactory。大部分都很顺利——编译时没有错误,但运行时出现了一个错误:
System.ArgumentException:“在注册的.NET数据提供程序列表中找不到指定的固定名称'System.Data.SqlClient'。”
我使用DbProviderFactories.GetFactoryClasses()检查是否安装了任何提供程序,但结果表中没有任何行。
所以我的问题是,我如何安装.NET Core的数据提供程序?我的机器上有.NET Framework 4.5,并且可以毫无问题地使用数据提供程序。但我不想为本地项目安装System.Data.SqlClient作为Nuget,因为这会增加使DbProviderFactory失去意义的依赖项。尽管如此,我已经尝试在使用我的库的测试项目中安装System.Data.SqlClient,但它仍然没有被捕获。

请查看以下链接:https://github.com/dotnet/core/issues/1699 - jdweng
你能提供更多的信息吗?您是如何安装 .net 的提供程序的?您是否将 DLL 包含在解决方案中并引用它,或者您是否在计算机上全局安装了它... - Joshua VdM
@JoshuaVdM - 这正是我的观点。我并没有明确安装任何东西。使用 .NET Framework,它会自动选择可用的数据提供程序。我不知道如何为 .NET Core 安装它们。即使我为 .NET Core 安装了 SqlClient Nuget,我仍然会遇到相同的错误。 - Allan Jardine
4个回答

20
在.NET Framework中,提供程序通过machine.config自动可用,也在GAC全局注册。 在.NET Core中,不再存在GAC或全局配置。 这意味着您需要首先在项目中注册提供程序,例如:
using System.Collections.Generic;
using System.Data.CData.MySQL; // Add a reference to your provider and use it
using System.Data.Common;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Register the factory
            DbProviderFactories.RegisterFactory("test", MySQLProviderFactory.Instance);

            // Get the provider invariant names
            IEnumerable<string> invariants = DbProviderFactories.GetProviderInvariantNames(); // => 1 result; 'test'

            // Get a factory using that name
            DbProviderFactory factory = DbProviderFactories.GetFactory(invariants.FirstOrDefault());

            // Create a connection and set the connection string
            DbConnection connection = factory.CreateConnection();
            connection.ConnectionString = "Server = test, Database = test";
        }
    }
}

正如您所看到的,我不得不添加对我的提供程序“System.Data.CData.MySQL”的引用。

很遗憾,您不能再获取所有可用的提供程序,但这就是我们在.NET Core中需要使用的。

(信息来自此 GitHub corefx 问题)


在 Asp.Net Core 层注册后,可以消除此错误,但现在我遇到了另一个错误。找不到名为 'System.Data.SqlClient' 的 ADO.NET 提供程序的 Entity Framework 提供程序。请确保在应用程序配置文件的 'entityFramework' 部分中注册了该提供程序。 - Ninos
@Ninos 随着Entity Framework的更新版本(如果我没记错,是6),连接和提供程序被分离了。现在,提供程序在entityFramework部分独立配置,与连接字符串无关。 - Suncat2000

5

正如Amer之前提到的:

在 .net core 中,您需要注册工厂

对于 SQL:DbProviderFactories.RegisterFactory("System.Data.SqlClient", SqlClientFactory.Instance);

但是,要做到这一点,您应该将 System.Data.SqlClient nuget 包添加到您的项目中。

像这样(工具 -> Nuget 包管理器 -> 包管理器控制台)

Find-Package SQLClient
Install-Package System.Data.SqlClient -ProjectName YourProjectName

谢谢!!!!!我只需要那一行代码,而不是Joshua VdM列出的所有内容。 - Keith Aymar

2
在 .net core 中,您需要注册工厂。
对于 SQL: DbProviderFactories.RegisterFactory("System.Data.SqlClient", SqlClientFactory.Instance);

0

我也遇到了这个问题,但我通过以下方法解决:

  1. 首先,您需要注册每个工厂:

    DbProviderFactories.RegisterFactory("System.Data.Obdc","System.Data.Odbc.OdbcFactory, System.Data.Odbc");

  2. DbProvider.FullTypeName 将自动成为您在 RegisterFactoryMethod 的第一个参数中插入的字符串。(在本例中为 System.Data.Odbc)


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