Unrecognized attribute 'name' error when r

2019-02-19 13:44发布

问题:

I am using Entity Framework 6 in my APS.NET 4.5 web application.

When running the following command:

using (var db = new booksEnteties())
{
    var books = from b in db.books
    select b;
} 

I get the following error:

An exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll but was not handled in user code

In the Details:

Unrecognized attribute 'name'.

And it pointing me to the web.config line 111:

 <remove name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" />

When I removed this line and try to run again, I get the following 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.

I assume that is something related to the Entity Framework 6 configuration in the web.config.

回答1:

As Tim Tang has stated, if you are using Entity Framework 6 for you need to use Connector/NET 6.8.x. You should also add a reference to the MySql.Data.Entity.EF6 to you project.

Then for your Web.config file, something like this:

 <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" 
                type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove>
      <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.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>

Look at this for more details.



回答2:

Just adding a summary of versions

  • For EF4, use Connector/NET 6.6.x (current GA is 6.6.6)
  • For EF5, use Connector/NET 6.7.x (current GA is 6.7.4) or Connector/NET 6.8.x (current GA is 6.8.3).
  • For EF6, use Connector/NET 6.8.x (current GA is 6.8.3).

in EF5 or less, all ok. in EF6, you need to use mysql connector 6.8.x, and add DbConfigurationTypeAttribute to you DbContext:

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class DemoContext : DbContext{}

which MySqlEFConfiguration is in MySql.Data.Entity.EF6.dll in 6.8.x. Have a try!



回答3:

The error is very specific... Its complaining about the attribute 'Name' Your problem is on this line:

<remove name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" />

Name is invalid here, just remove it so it should be like this:

<remove invariant="MySql.Data.MySqlClient" />