0

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>

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Your solution resolved the posted issue. But GetConnectionString(true) method is throwing an exception "The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception." . I know this is a different issue.
Thanks for your help, I forgot to remove the above app.config settings and that was causing the ReplicationManager exception. After removing the app.config, its working now!
@Ullan That error has been reported frequently against MySql.Data (e.g., stackoverflow.com/a/65496771/23633, stackoverflow.com/a/60786792/23633) which is why I recommended MySqlConnector for .NET Core instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.