找不到与不变名称为“System.Data.SqlServerCe.4.0”的ADO.NET提供程序相关的Entity Framework提供程序。

36

当我使用sqlce 4.0和entityframework 6.0时,我遇到了以下错误。

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlServerCe.4.0'

我的 app.config 文件看起来像这样

....
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, 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" />
    </providers-->
  </entityFramework>
  <connectionStrings>
    <add name="FbMultipleInsOrderContainer" connectionString="metadata=res://*/FbMultipleInsOrder.csdl|res://*/FbMultipleInsOrder.ssdl|res://*/FbMultipleInsOrder.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;data source=|DataDirectory|\FBMultipleOrderSync.sdf&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
...

我尝试重新安装EF 6,但无济于事。

对此有任何线索将不胜感激。


2
你安装了EntityFramework.SqlServerCompact包吗? - ErikEJ
2
你需要一个 EntityFramework.SqlServerCompact 的提供程序条目,安装 NuGet 包将会添加它。 - ErikEJ
1
现在我遇到了以下问题:应用程序配置文件中为ADO.NET提供程序注册的实体框架提供程序类型'System.Data.Entity.SqlServerCompact.SqlCeProviderServices,EntityFramework.SqlServerCompact'(不变名称为'System.Data.SqlServerCe.4.0')无法加载。 - kuhajeyan
2
我已经解决了,我必须再次将这些包添加到我的消费者项目中。 - kuhajeyan
  1. 在Visual Studio解决方案资源管理器中选择您的解决方案
  2. 转到工具 -> 库程序包管理器 -> 程序包管理器控制台
  3. 一个窗口将出现在Visual Studio底部。
  4. 输入此命令:install-package EntityFramework.SqlServerCompact
  5. 按Enter键。这将安装EntityFramework.SqlServerCompact Nuget包,从而解决此问题。
- user3352330
12个回答

59
在安装EntityFramework.SqlServerCompact nuget包之后,请检查您的app.config文件是否包含以下内容(根据@ErikEJ上面的评论):
<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>

即使您实际上不需要它,也可以保留SqlClient这一行。


21

ErikEJ已经指出了,但我想强调一点,你必须记得安装Nuget包。

EntityFramework.SqlServerCompact

我尝试按照推荐答案操作,但没有意识到我没有所需的NuGet包被App.config引用。


给这个点赞!..这真的应该添加到主要答案中!我会请求编辑。 - sports

6

我在单元测试中遇到了问题。麻烦的是,错误并不总是出现。

我通过将以下类添加到我的单元测试解决方案中成功解决了这个问题:

public static class Trick
{
    public static void FixEfProviderServicesProblem()
    {
        // this commented line should be used for SQL Server provider
        //var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;

        // this is used for SQL Server CE provider
        var instance = System.Data.Entity.SqlServerCompact.SqlCeProviderServices.Instance;
    }
}

5
为了实现这一点,您只需要在包管理器控制台中注册 EntityFramework.SqlServerCompact
要执行此操作,请打开Power Manager 控制台并输入以下命令:
PM> Install-Package EntityFramework.SqlServerCompact

2
为了解决这个问题,
请按照以下步骤操作:
1. 打开 Visual Studio 菜单
2. 点击 工具 (Tools) 选项
3. 选择 NuGet 包管理器 (NuGet Package Manager)
4. 在“浏览”(Browse)选项卡中搜索 "EntityFramework.SqlServerCompact"
5. 安装它
6. 完成以上步骤后,应该就可以正常工作了。

1
这个错误出现在用户的电脑上。用户忽略了之前的一个错误“‘DbProviderFactories’部分每个配置文件只能出现一次”。
显然,DB2在她的电脑上破坏了机器配置文件,通过添加一个重复的(且为空的)<DbProviderFactories />标签。
解决方法是删除空的重复标签。机器配置文件位于[WindowsDir]\Microsoft.Net\Framework[.NET Version]\Config\Machine.config。

1
我的问题在于单元测试。你可能只为你的项目安装了Entity Framework,但没有为你的单元测试安装。要这样做:
在解决方案资源管理器中右键单击“解决方案”(而不是你的项目名称)
点击“管理 NuGet 包”
搜索 EntityFramework 并按下它
你可能看不到 [Project Name].Tests 旁边的勾号

1
提供的解决方案是正确的,但没有解释为什么EF需要这个程序集。以下是原因...
默认情况下,EF的配置允许您在entityFramework部分下拥有"defaultConnectionFactory"。该默认工厂已配置为"LocalDbConnectionFactory",其参数为"mssqllocaldb"。
此工厂在内部需要"SqlServerCe.4.0"即"SqlServerCompact"。
如果EF在DbContext中使用默认连接字符串,则仅会发生错误。如果EF在配置文件中提供了其他指定的连接字符串,则不会出现此错误。因为EF默认使用默认连接工厂。
安装"SqlServerCe 4.0"后,EF的配置将更改,并且"LocalDbConnectionFactory"及其参数"mssqllocaldb"将被"SqlServer Ce 4.0"替换。EF运行时也能够找到SqlServerCe4.0程序集(因为它现在是您引用的一部分,并且物理放置在BIN中)。
以下是安装Sql Server Compact之前和之后的EF配置
旧配置:
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="mssqllocaldb" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>

新配置如下:

<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>
  </entityFramework>

同时,它添加了以下部分来描述新的工厂

<system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>

由于我没有使用Entity Framework,而是使用一个可能比Entity Framework更早的旧库,这就是解决我的问题的方法。谢谢! - Steve Hiner

0
在Visual Studio中,所有存在于“_bin_deployableAssemblies”文件夹中的“.dll”文件都需要设置为“Build Action: none”。
对我来说,所有的.dll文件都被设置为“content”。
这就是我所设置的,它可以直接工作:

enter image description here


0

对我来说,因为程序员不只一个,有些程序员已经安装了EntityFramework.SqlServerCompact,但当我在我的电脑上拉取并运行时出现上述错误信息,那么我只需要卸载并重新安装即可。


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