Cant get my connection to a MySQL from asp.net mvc project to work
I am trying to get MVC 3 Application to use a MySql database, but I keep getting errors, I have made the following co开发者_如何学Gonnectionstring and provider in my Web.Config file
<connectionStrings>
<add name="DefaultConnection" connectionString="server=xxxxx.unoeuro.com; userid=xxxxx_dk;password=xxxxx;pooling=yes;Database=xxxxx_dk_db" providerName="MySql_db"/>
</connectionStrings>
<system.data>
<DbProviderFactories>
<add name="MySql_db" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.1.54.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>
But when I try to use it (Just trying to use the default register user that is in a default project) I get this error
An error occurred creating the configuration section handler for system.data: Column 'InvariantName' is constrained to be unique. Value 'MySql.Data.MySqlClient' is already present.
But I if I remove the DbProviderFactories I just get an error that there is missing a data provider, so its not becuase I duplicated it anywhere
Building upon @Ladislav Mrnka's answer, the error is probably occurring because the setting in Web.config is conflicting with one in machine.config. To eliminate the error, add a "remove" tag to your Web.config as follows.
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"/>
<add name="MySql_db" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.1.54.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>
Provider name in connection string must be set to invariant name of provider factory. Also the error most probably says that you already have MySql.Data.MySqlClient
installed on your machine and registered in Machine.config.
Install the latest version of MySQL for visual studio and find a compatible version for Connector/NET.
For example, I have used MySQL for Visual Studio 1.2.7. and Connector/NET 6.7.5, 6.8.3 or 6.9.x. for Visual Studio 2013
uninstall all the other versions of MySQL for visual studio and Connector/NET.
After that Add new item-> ADO.Net Entity Data Model and create the edmx file. you can see system.data and system.data.entity in your reference folder of the project.
In your code MyDatabaseEntities myDB = new MyDatabaseEntities(); use myDB to access your data.
If you are creating a framework project, copy everything under tag from app.config and paste it into your main project's(startup project) web.config file One thing needs to keep in mind that this tag should be the first child of tag.
精彩评论