Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

c# - .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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...