简体   繁体   English

实体框架使用错误的连接提供程序

[英]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.我正在尝试将 Entity Framework 5(代码优先)用于 ASP.NET Web 窗体应用程序,我以前有一个使用 MySQL 连接运行的基本实现,我也成功地使用 SQL Server Compact 4 处理桌面应用程序.

The problem I am having is that with EF5 I am getting the error我遇到的问题是使用 EF5 时出现错误

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升级到 EF6-rc1 给了我更有用的错误

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.具有不变名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序未在计算机或应用程序配置文件中注册,或者无法加载。 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.我的问题是我已经从项目中删除了所有 MySQL 的痕迹,项目没有理由尝试加载 MySQL 提供程序。

<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.这些是我的 web.config 文件的摘录,如果有任何其他相关部分可能指向解决方案,请告诉我。

I have done a text search through every file in the solution for the word MySQL and there are no references to MySQL.我已经在解决方案中的每个文件中对 MySQL 一词进行了文本搜索,并且没有对 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?任何人都可以提供有关我应该在哪里查看以确定 EF 为何坚持加载 MySQL 提供程序的任何线索?

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.数据库文件被正确创建,看起来EF然后拒绝使用它并尝试使用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.我在从 Oracle 过渡到 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.我在<connectionStrings></connnectionStrings>部分的 web 配置中列出了我的连接字符串。 It seemed to be reading the connection string, but not the providerName .它似乎正在读取连接字符串,但不是providerName

I looked at it for a long time before realizing that I had needlessly put it inside the <runtime></runtime> section.我看了很长时间才意识到我不必要地把它放在了<runtime></runtime>部分。

When I put my <connectionStrings> back in the root of <configuration> , Entity Framework started using the right provider again.当我将<connectionStrings>放回<configuration>的根目录时,Entity Framework 再次开始使用正确的提供程序。

I had the same issue recently, There is an issue in your App.config file.我最近遇到了同样的问题,您的 App.config 文件中存在问题。 Just recreate it and it will work.只需重新创建它,它就会起作用。 (Create an black project and copy its default app.config and replace it). (创建一个黑色项目并复制其默认的 app.config 并替换它)。 Then re-install the nuget packages.然后重新安装nuget包。

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.我最终创建了一个新的解决方案并复制了所有源代码(web.config 和 Migrations 除外)并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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