开发者

"Failed to find or load the registered .Net Framework Data Provider" with MySQL + ASP.NET

How do we repair this? This question has been sort of addressed many times around the internet, but it's always a workaround. Always copying the MySql.data.dll into your bin directory, or explicitly stating what version you want. What is the "proper" approach to using DbProvderFactory for MySQL with ASP.NET?

I'd l开发者_JAVA百科ike to be able to develop locally and not worry what version they have installed on the server. As it stands, if I do copy up my own version I have to make sure it's the one they use. Seems easy to break.


If the assembly in question is not in the GAC, then it does have to exist in the path, i.e. the bin folder. The be in the GAC, the assembly has to be signed. I'm guessing the the builders of MySql.data.dll did not sign the dll so you may not be able to put it into the GAC on the server. So copying the dll is your solution.


Another reason for this error was noted by a user on a similar thread here I have copy pasted the users answer below in case the link changes

I just saw that the reason of this exception in my case were the differt versions between dll and the configuration entry. So, sometimes the dll you actually have (either installed by nuget or other way) differs from the entry in the node of the app.config. This section, if not in app.config, can be also found under Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config and nearby folders. Changing version in the entry to version of the dll solved the issue.


Here how I got the MySQL connector to work:

In the machine.config under DbProviderFactories add the folliwng:

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

*Note the version number and PublicKeyToken will be different depending on the version of the MySQL connector you have installed. This example is for v6.7.4.0 and v2.0 dot net

Next copy %Program Files%\MySQL\MySQL Connector Net 6.7.4\Assemblies\v2.0\MySql.Data.dll to %Windows\assembly.

Hope this helps someone :)


Another way through code without app.config: (don't forget to set the right MySql.Data version in this code)

  public class MySqlDbConfiguration: DbConfiguration 
{
    public MySqlDbConfiguration()
    {
        SetDefaultConnectionFactory(new MySqlConnectionFactory());
        SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());
        RegisterFactoryIfRequired();
    }

    private static void RegisterFactoryIfRequired()
    {
        string dataProvider = @"MySql.Data.MySqlClient";
        string dataProviderDescription = @".Net Framework Data Provider for MySQL";
        string dataProviderName = @"MySQL Data Provider";
        string dataProviderType =
            @"MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d";

        bool addProvider = true;
        var dataSet = ConfigurationManager.GetSection("system.data") as DataSet;
        foreach (DataRow row in dataSet.Tables[0].Rows)
        {
            if ((row["InvariantName"] as string) == dataProvider)
            {
                // it is already in the config, no need to add.
                addProvider = false;
                break;
            }
        }

        if (addProvider)
            dataSet.Tables[0].Rows.Add(dataProviderName, dataProviderDescription, dataProvider, dataProviderType);

        // test it
        var factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜