.NET MySQL Connector Conflicting DbProviderFactories

I'm using the .NET MySQL Connector with Entity Framework 4, and everything worked great but I wanted to package the MySQL client DLL files with my application when deployed on servers so we don't have to worry about installing mysql on each server, each application will just have the correct copy that it needs.

To make this possible, I made sure the MySQL references had "Copy Local" set so they would be copied to the bin folders and added the following to my app.config:

<system.data>
    <DbProviderFactories>
      <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.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

This works and I am able to deploy the app without installing mysql on the remote server, but now I have a problem on my local dev machine(where I do have the MySQL connector installed), and I'm getting this error when EF tries to connect:

Column 'InvariantName' is constrained to be unique. Value 'MySql.Data.MySqlClient' is already present.

If I comment out the XML I added above in the app.config, the error goes away. This is likely because the same driver is installed on the system and is in the machine.config.

What is the solution? I would rather not have to manually comment and uncomment the line depending on which system I build the application for.


try to add < remove invariant="MySql.Data.MySqlClient" / > in your webconfig. On your computer, you have installed the Connector for MySql and your machine.config have been modified by adding an item in the DbProviderFactories. So if you put another MySql Data Provider in your web.config, its like if you are trying to register the same thing twice.

<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=6.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

You can also consider to copy all mysql DLLs from C:Program Files (x86)MySQLMySQL Connector Net 6.8.3Assembliesv4.0 into your project's bin folder. By that way EF6 can reach all required MySQL connector(MySql.Data) sub-classes.

链接地址: http://www.djcxy.com/p/60968.html

上一篇: MySQL连接器.NET和Unicode

下一篇: .NET MySQL连接器冲突的DbProviderFactories