Search code examples
c#mysql.net-core-3.0.net-standard-2.1

The specified invariant name 'MySql.Data.MySqlClient' wasn't found in the list of registered .NET Data Providers


I have a C# Library developed using .Net Standard 2.1 and this library is called in a test console application built on .Net Core 3.0. But at run time, the GetFactory is throwing an exception.

 private DbConnection connection;
 connection = DbProviderFactories.GetFactory("MySql.Data.MySqlClient").CreateConnection();

Exception:

DbProviderFactories.GetFactory The specified invariant name 'MySql.Data.MySqlClient' wasn't found in the list of registered .NET Data Providers.

I have installed MySqlConnector 1.2.1. But it didn't help. And also adding the below in app.config file.

<system.data>
 <DbProviderFactories>
  <remove invariant="MySql.Data.MySqlClient" />
  <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=8.0.23.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>

Solution

  • The XML you've shown for app.config only works in .NET Framework applications.

    For .NET Core 2.1 or later, call DbProviderFactories.RegisterFactory. The exact arguments you pass will depend on the factory you want to register (and the NuGet packages you have installed):

    // MySqlConnector (my personal recommendation for .NET Core)
    DbProviderFactories.RegisterFactory("MySqlConnector", MySqlConnector.MySqlConnectorFactory.Instance);
    
    // MySql.Data (may be needed instead if other code is hard-coded to "MySql.Data.MySqlClient")
    DbProviderFactories.RegisterFactory("MySql.Data.MySqlClient", MySql.Data.MySqlClient.MySqlClientFactory.Instance);
    

    For more information, see Using DbProviderFactories.