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)
Let us consider two approaches for a solution.
- Download and install MySQL Connectors such that they are in the development environment GAC.
- 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:
<add name="MyDatabase" connectionString="..." providerName="MySql.Data.MySqlClient"/>
Here, we have a reference to an Assembly Namespace which does not exist.
The method to use this first approach is as follows:
- Download and install MySQL Connectors.
- Copy installed .NET Connector from “C:WindowsMicrosoft.NETassemblyGAC_MSIL” to your project ~bin folder.
- MySQL.Data
- MySQL.Web
- MySQL.Data.Entity
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.
- 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’.
- Add project reference if calls are made in code.
- 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:
<system.data> <DbProviderFactories> <clear /> <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient.NoGAC" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data>
- Add Assembly Compilation references where here we need to set the Version number to that noted (“6.4.4.0”) above:
<compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="Mysql.Data, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/> <add assembly="Mysql.Data.Entity, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/> <add assembly="Mysql.Web, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/> </assemblies> </compilation>
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.
- Download and compile the Connectors.
- Copy the Release build of the Connectors to your Web Projects bin folder.
- 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’.
- Add project references if calls are made in code.
- Modify the Web.config
- Add the following where we need to set the Version number to that noted (“6.4.4.0”) above:
<system.data> <DbProviderFactories> <clear /> <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.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data>
- Add Assembly Compilation references where here we need to set the Version number to that noted (“6.4.4.0”) above:
<compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="Mysql.Data, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/> <add assembly="Mysql.Data.Entity, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/> <add assembly="Mysql.Web, Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/> </assemblies> </compilation>
- Add the following where 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
- MySQL Connector Downloads
- MySQL Tutorial: Using an Entity Framework Entity as a Windows Form Data Source
- GoDaddy: How do I recompile the MySQL library to run under medium trust? [Not necessary for this solution but a good to know!]
Done as same as above but still facing the same issue “Unable to find the requested .Net Framework Data Provider. It may not be installed.”