问题描述
我最近将旧项目中的实体框架从版本 4 或 5 升级/更新到版本 6.现在我得到了这个异常:
I recently upgraded/updated Entity Framework in an old project from version 4 or 5 to version 6. Now I get this exception:
System.InvalidOperationException"类型的异常发生在EntityFramework.dll 但未在用户代码中处理
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
附加信息:未找到适用于具有不变名称System.Data.SqlClient"的 ADO.NET 提供程序.制作确保提供者在 'entityFramework' 部分中注册应用程序配置文件.看http://go.microsoft.com/fwlink/?LinkId=260882 了解更多信息.
Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
我用谷歌搜索了这个错误并遇到了几个 SO 线程,但没有一个包含适合我的解决方案.这就是我的 App.config 的样子:
I googled the error and came across a couple of SO threads, but none of them contained a solution that works for me. This is what my App.config looks like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
我已经从我的项目中卸载了 Entity Framework 并重新安装了它,删除了对旧 EF 文件的所有引用并重新安装,但对我没有任何作用.我不断收到此错误.
I already uninstalled Entity Framework from my project and re-installed it, deleted all the references to old EF files and re-installed, but nothing works for me. I keep getting this error.
推荐答案
好吧,这很奇怪.我有几个项目:一个是 UI 项目(一个 ASP.NET MVC 项目),另一个是存储库等项目.存储库项目引用了 EF,但 UI 项目没有(因为它不需要一个,它只需要引用其他项目).在我也为 UI 项目安装了 EF 之后,一切都开始工作了.这就是为什么它将这段代码添加到我的 Web.config 中:
Ok, this is pretty weird. I have a couple of projects: one is a UI project (an ASP.NET MVC project) and the others are projects for stuff like repositories. The repositories project had a reference to EF, but the UI project didn't (because it didn't need one, it just needed to reference the other projects). After I installed EF for the UI project as well, everything started working. This is why, it added this piece of code to my Web.config:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
这篇关于错误:找不到具有不变名称“System.Data.SqlClient"的 ADO.NET 提供程序的实体框架提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!