简体   繁体   中英

Entity Framework using the wrong connection provider

I am trying to use Entity Framework 5 (code-first) for an ASP.NET Web Forms application, I have previously had a basic implementation running using the MySQL connection and I have also had success working with a desktop application using SQL Server Compact 4.

The problem I am having is that with EF5 I am getting the error

The specified store provider cannot be found in the configuration, or is not valid.

which is very little help.

Upgrading to EF6-rc1 gives me the more useful error of

The ADO.NET provider with invariant name 'MySql.Data.MySqlClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.

At least I now know what the "problem" is.

The issue I have is that I have removed all trace of MySQL from the project, there is no reason why the project should be trying to load the MySQL provider.

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="System.Data.SqlServerCe.4.0" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>

<connectionStrings>
<add name="im_customerdb" connectionString="Data Source=|DataDirectory|\CustomerData.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

These are excerpts from my web.config file, please let me know if there are any other relevant sections that might point at the solution.

I have done a text search through every file in the solution for the word MySQL and there are no references to MySQL. I have checked a clean version of the project out of source control to be sure that I haven't missed a file when purging the project build files.

Can anyone provide any clues as to where I should look to determine why EF is insisting on loading a MySQL provider?

Regards, Anthony

* Edit: * Just to add that the exception is thrown the first time the database context is accessed, from the stack trace the exception is being thrown when the database is being initialised. I've also noticed something very peculiar; the database file is being created correctly, it just seems that EF then refuses to use it and tries to use MySQL.

Questions is old, but may be this will be helpful for somebody.

I've got the same issue with transition from Oracle to MS SQL Server. Eventually, it appeared that code migrations were the root cause of the issue. You cannot just leave them as is after switching to another database provider. So, you must REGENERATE migrations for your new database provider.

I had the same issue with a different resolution, so I'll go ahead and post it here for the next person who searches.

I had my connection string listed where it could be found in the web config in the <connectionStrings></connnectionStrings> section. It seemed to be reading the connection string, but not the providerName .

I looked at it for a long time before realizing that I had needlessly put it inside the <runtime></runtime> section.

When I put my <connectionStrings> back in the root of <configuration> , Entity Framework started using the right provider again.

I had the same issue recently, There is an issue in your App.config file. Just recreate it and it will work. (Create an black project and copy its default app.config and replace it). Then re-install the nuget packages.

All the best.

None of the answers above solved the problem for me. I ended up creating a new solution and copied all the source (except web.config and Migrations) over and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM