MySQL.Data, MySQL.Web or MySQL.Data.Entity not available with Hosting Provider

Some hosting providers, such as GoDaddy, do not include MySQL.Data, MySQL.Web or MySQL.Data.Entity in their GAC (Global Assembly Cache).  If you are unaware of this you might encounter one of the below errors.

Object reference not set to an instance of an object.

[NullReferenceException: Object reference not set to an instance of an object.]
MySql.Data.MySqlClient.MySqlClientFactory.get_MySqlDbProviderServicesInstance()

Unfortunately I don’t have an image illustrating the yellow screen of death.

Unable to find the requested .Net Framework Data Provider. It may not be installed.

[ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.]
System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)

MySQL Provider Error
Let us consider two approaches for a solution.

  1. Download and install MySQL Connectors such that they are in the development environment GAC.
  2. Download, compile and add Web.config references .NET MySQL Connector

Considering the first approach, no references have to be made in the development environment.  Essentially start a new solution, a new web project and start coding away.  However at production deployment time, at the hosting provider, you will most likely encounter the second error of “Unable to find the requested .Net Framework Data Provider.  It may not be installed.”  Which makes sense.  There is a reference, probably in your Web.config, to a MySQL connector.  Perhaps the most common reference would be the connection string:

Here, we have a reference to an Assembly Namespace which does not exist.

The method to use this first approach is as follows:

  1. Download and install MySQL Connectors.
  2. Copy installed .NET Connector from “C:WindowsMicrosoft.NETassemblyGAC_MSIL” to your project ~bin folder.
    • MySQL.Data
    • MySQL.Web
    • MySQL.Data.Entity

    .NET MySQL Connectors
    Now would be a good time to right click each file, choose ‘Properties’, under the ‘Details’ tab and note the version number. In the below image we’ll note my ‘Product version” of “6.4.4.0” which we’ll set in the Web.config file in a moment. Remember, your version might be different as this is the latest at the time of this post.
    MySQL Data dll Version

  3. Within Visual Studio’s Solution Explorer, highlight your project and select ‘Show All Files’. Within the ‘bin’ folder right click each MySQL.*.dll and select ‘Include In Project’. Then, right click each again and select ‘Properties’ where you’ll change the ‘Copy to Output Directory’ to ‘Copy always’.
    Visual Studio Copy dll To Output Folder
  4. Add project reference if calls are made in code.
  5. Modify the Web.config
    • Append “.NoGAC” to the connection string providerName (e.g. providerName=”MySql.Data.MySqlClient.NoGAC”) to explicitly not use GAC version.
    • Add the following where we need to set the Version number to that noted (“6.4.4.0”) above:

       

    • Add Assembly Compilation references where here we need to set the Version number to that noted (“6.4.4.0”) above:

       

That’s it, Publish and Deploy.

Connector installation GOTCHA!!  If you use the local GAC during development then a different version of the MySql Connectors in production via Web.config you will have issues.  Just make sure the version match.

As for the second approach and perhaps my preferred method, is to create an environment which is similar to that of the production environment at the hosting provider.

  1. Download and compile the Connectors.
  2. Copy the Release build of the Connectors to your Web Projects bin folder.
  3. Within Visual Studio’s Solution Explorer, highlight your project and select ‘Show All Files’. Within the ‘bin’ folder right click each MySQL.*.dll and select ‘Include In Project’. Then, right click each again and select ‘Properties’ where you’ll change the ‘Copy to Output Directory’ to ‘Copy always’.
    Visual Studio Copy dll To Output Folder
  4. Add project references if calls are made in code.
  5. Modify the Web.config
    • Add the following where we need to set the Version number to that noted (“6.4.4.0”) above:

       
    • Add Assembly Compilation references where here we need to set the Version number to that noted (“6.4.4.0”) above:

       

And that’s it for the second method.

The last thing I should mention is technically to use MySql all you need is the MySql.Data Assembly.  However, for completeness I chose to include all three assemblies available for the .Net Connector.

References