在 .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 核心中使用的内容。
(来自此 GitHub corefx 问题的信息)