实体框架中未找到 ADO.NET Sql.Data.Client 提供程序。

19

我有一个类似于在这个问题中提出的问题“找不到具有不变名称'System.Data.SqlClient' 的ADO.NET提供程序的Entity Framework提供程序”,错误消息如下:

"具有不变名称'System.Data.SqlClient'的ADO.NET提供程序未在机器或应用程序配置文件中注册,或者无法加载。请参阅内部异常以了解详细信息。"

正如相关问题的答案所建议的那样,我已经通过包管理器控制台重新安装了Entity Framework(EF6),但错误仍然存在。 我还检查了EntityFramework.SqlServer.dll是否被引用到我的项目中。 以下是存储在App.config中的连接字符串:

<add name="DesignModel"    ConnectionString="metadata=res://*/DesignModel.csdl|res://*/DesignModel.ssdl|res://*/DesignModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=C071E;initial catalog=CTD2;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />     

我有另一个项目,其中我使用EntityFramework创建了完全相同的实体和上下文,并且它能正常工作,这让问题变得更加令人困惑。

当尝试执行以下行时显示错误:

DesignModel designContext = new DesignModel();
designContext.MoPerfIDs.Load();

DesignModel是继承DbContext的类名。

这是完整的App.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
       <section name="ppe.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    <!-- 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>

  <connectionStrings>
    <add name="DesignModel" connectionString="metadata=res://*/DesignModel.csdl|res://*/DesignModel.ssdl|res://*/Design    Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MONNMC071E;initial catalog=CTD2;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="System.Data.SqlClient" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>
</configuration>

非常感谢您的帮助,提前致谢。


内部异常是什么? - Pawel
1
实体框架的SqlServer.dll是否在bin目录中? - Jace Rhea
3
我遇到完全相同的问题。我不能简单地创建另一个解决方案来解决这个问题。我也不明白为什么这被关闭为“不相关的话题”。 - Mike Becatti
这似乎不是无关话题的问题。这个问题已经有1.5万个浏览量了!我也遇到了同样的问题,但我无法解决它。在我看来,这是MySQL和EntityFramework的问题,可能可以通过某种方式绕过。提供的答案对我没有帮助。我希望这个问题能够重新开放,我们可以一起找到解决办法。 - Tomas Kubes
@Pawel:我想检查内部异常,但不知道如何操作。错误消息出现在按下构建(F6)后的错误窗口中。 - Tomas Kubes
9个回答

3
您需要为System.Data.SqlClient SQL连接类型注册Entity Framework提供程序。 在app.config文件中应该有以下内容:
<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.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

OP没有使用LocalDb,请检查EF连接字符串。 - Aron
谢谢两位的回复。我按照建议尝试了,但是仍然出现相同的错误。我编辑了问题并发布了完整的App.config文件。非常困惑,因为在另一个项目中,没有出现错误的唯一事项就是App.config中只有用于实体框架的连接字符串。 - MigLuev
@MigLuev:这对我也没有帮助。你最终解决了吗? - Tomas Kubes

1

只需从NuGet安装"MySql.Data.Entity"!它会自动安装MySQL ADO驱动程序和实体驱动程序!


1

仍然不确定是什么导致了问题,但最终创建了一个新的解决方案,并将我的项目中的所有内容复制过去。现在一切正常。确实非常奇怪。


0

我遇到了这个错误,但对我来说它完全不同。

我必须编辑:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config

并且:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config

正在搜索 DbProviderFactories,两个配置文件看起来像这样:

<system.data>
    <DbProviderFactories>
        <add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26" />
    </DbProviderFactories>
    <DbProviderFactories />
</system.data>

当我删除尾随的<DbProviderFactories />时,一切都重新开始工作了。

我能够通过仅查看无法找到所请求的 .Net Framework 数据提供程序并找到这个答案来解决它:

https://dev59.com/_mkw5IYBdhLWcg3wZJl9#9929534


0

哇...那真是非常特别...无论如何。您需要为 System.Data.SqlClient SQL 连接类型注册 Entity Framework 提供程序。

您想要将以下内容添加到您的 app.config/web.config 文件中:

<configuration>
  <configSections>
    <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">
      <parameters>
        <parameter value="System.Data.SqlClient" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

不,RajeshKannan,它没有。 - Mike Becatti

0

有两个选项可以尝试。

1)手动加载 "System.Data.SqlClient"

在您的上下文类中包含以下语句

var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);

2)不要加载 "System.Data.SqlClient"

通过更改从

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>

希望你的问题已经解决了。


这些建议都没有解决问题。 - Mike Becatti

0
尝试在designContext的ctor中添加以下内容:

var _ = System.Data.Entity.SqlServer.SqlProviderServices.Instance;


0
在我的情况下,问题是我为了捕获另一个异常启用了CLR异常。但我忘记了将其禁用。
我在我的异常设置中禁用了它。它忽略了这个异常并继续运行,并自动为我创建了数据库(在我的情况下)。

-1

如果您在使用Visual Studio进行调试时遇到此错误,那么这可能是由于您安装了另一个数据库提供程序或带有数据库提供程序的IDE所致。我在计算机上安装Delphi后遇到了这个错误。

解决方案:只需编辑C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config文件并删除标签即可。


这与此答案有何不同? - Gert Arnold
因为我解释了原因。 - H. Aydin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接