Simon
Simon

Reputation: 305

Switching from MySQL ADO.NET libraries to MySQL connector issue

I have switched my C# project from MySQL ADO.NET libraries to MySQL connector since there was a bug in MySQL libraries.

Previous Question related to the aforementioned issue: StackOverFlow Question

I have used MySql Connector documentation as the reference for switching libraries.

Following the change of the libraries, there were some new errors triggered for the MySQL keywords in the existing project.

1) MySqlConnection in private MySqlConnection connection; line.

Error: Represents an open connection to a MySQL Server database. This class cannot be inherited. The type 'MySqlConnection' exists in both 'MySql.Data' and 'MySqlConnector'.

2) MySqlException in catch (MySqlException ex) line.

Error: The Exception is thrown when MySQL returns an error. This class cannot be inherited. The type 'MySqlException' exists in both 'MySql.Data' and 'MySqlConnector'.

3) MySqlCommand in MySqlCommand cmd = new MySqlCommand(); line.

Error: Represents a SQL Statement to execute against a MySQL database. This class cannot be inherited. The type 'MySqlCommand' exists in both 'MySql.Data' and 'MySqlConnector'.

4) MySqlDbType in cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value = (values[0]); line.

Error: Specifies MySQL specific data type of a field, property, for use in a MySql.Data.MySqlClient.MySqlParameter. The type 'MySqlDbType' exists in both 'MySql.Data' and 'MySqlConnector'.

Any suggestions on how to mitigate the above errors?

Upvotes: 0

Views: 1583

Answers (2)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

The type 'MySqlConnection' exists in both 'MySql.Data' and 'MySqlConnector' indicates the type has present in both assemblies, which causing ambiguous reference. Normally you can simply remove MySql.Data.dll references using NuGet package manager with Uninstall-Package MySql.Data command (and removing all possible traces of that assembly) then reference issue should be resolved, but in certain conditions you can do steps below:

  1. Go to reference properties and set alias MySqlConnector for assembly before all using statements, e.g. extern alias MySqlConnectorAlias;.

  2. Use reference from the alias on step (1), e.g. MySqlConnectorAlias::MySql.Data.MySqlClient.MySqlConnection.

  3. (For .NET Core only) Before using steps (1) and (2), edit the .csproj file which contains ChangeAliasesOfStrongNameAssemblies like this:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
       <!-- other stuff -->
    
       <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
           <ItemGroup>
               <ReferencePath Condition="'%(FileName)' == 'MySqlConnector'">
                   <Aliases>MySqlConnectorAlias</Aliases>
               </ReferencePath>
           </ItemGroup>
      </Target>
    
      <!-- other stuff -->
    
    </Project>
    

Upvotes: 2

Bradley Grainger
Bradley Grainger

Reputation: 28207

The primary error is:

The type 'MySqlConnection' exists in both 'MySql.Data' and 'MySqlConnector'.

It appears that your project is referencing both MySql.Data.dll and MySqlConnector. Uninstall the MySql.Data package using NuGet Package Manager, and make sure the MySql.Data.dll reference is removed from your csproj, packages.config etc.

(In advanced scenarios, you can use an extern alias to reference both libraries, but I would strongly discourage it: it can cause two connection pools to be created, two cleanup threads to be started, etc.)

Upvotes: 2

Related Questions