在.NET Core 2.1中安装.NET SQL Client之后,DbProviderFactories.GetFactoryClasses不返回任何结果 [英] DbProviderFactories.GetFactoryClasses returns no results after installing .NET SQL Client in .NET Core 2.1

查看:46
本文介绍了在.NET Core 2.1中安装.NET SQL Client之后,DbProviderFactories.GetFactoryClasses不返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在将库移植到.NET Core 2.1,因为它已支持DbProviderFactory.在大多数情况下,它都很好-可以编译,但是在运行时出现错误:

I'm porting a library over to .NET Core 2.1 now that it has support for DbProviderFactory. For the most part it has gone fine - it compiles, but when run I get an error:

System.ArgumentException:'在注册的.NET数据提供程序列表中找不到指定的不变名称'System.Data.SqlClient'.'

System.ArgumentException: 'The specified invariant name 'System.Data.SqlClient' wasn't found in the list of registered .NET Data Providers.'

我使用了 DbProviderFactories.GetFactoryClasses()来检查是否安装了任何提供程序,并且似乎没有(提供的表中有0行).

I've used DbProviderFactories.GetFactoryClasses() to check if there are any providers installed, and there doesn't appear to be (0 rows in the resulting table).

所以我想我的问题是,如何为.NET Core安装数据提供程序?我在计算机上安装了.NET Framework 4.5,它正在拾取数据提供程序而没有任何问题.我不想将 System.Data.SqlClient 作为本地项目的Nuget安装,因为那样会添加依赖项,从而使 DbProviderFactory 成为问题.就是说,我试图在使用我的库作为测试的项目中安装 System.Data.SqlClient ,但仍然没有被选择.

So I guess my question is, how can I install the data providers for .NET Core? I've got .NET Framework 4.5 on the machine and it is picking up the data providers without any issue. I don't want to install System.Data.SqlClient as a Nuget for the local project, since that would add a dependency that would make the DbProviderFactory irrelevent. That said, I have attempted to install System.Data.SqlClient in a project that uses my library as a test, and it still isn't picked up.

推荐答案

在.NET Framework中,提供程序可以通过machine.config自动获得,也可以在GAC中进行全局注册.在.NET Core中,不再有GAC或全局配置.这意味着您必须首先在项目中注册提供商,如下所示:

In .NET Framework, the providers are automatically available via machine.config and are also registered globally in the GAC. In .NET Core, there is no GAC or global configuration anymore. This means that you'll have to register your provider in your project first, like so:

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"的引用.

As you can see, I had to add a reference to my Provider, "System.Data.CData.MySQL" in this case.

可悲的是,您不能再获得所有可用的提供程序,但这是我们必须在.NET core中使用的功能.

It's sad that you can't just get all available providers anymore, but this is what we have to work with in .NET core.

(信息来自此GitHub corefx问题)

这篇关于在.NET Core 2.1中安装.NET SQL Client之后,DbProviderFactories.GetFactoryClasses不返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆