没有找到与不变名称为“System.Data.SqlClient”的ADO.NET提供程序对应的Entity Framework提供程序。

36
我们正在使用EntityFramework 6和Code First。我们有一个控制台应用程序,没有引用EntityFramework但从其App.config中读取连接字符串。它调用DatabaseInitializationUtilities程序集并将连接字符串作为参数传递。
DatabaseInitializationUtilities引用了EF6(EntityFramework和EntityFramework.SqlServer)。它的App.config是这样的:
<?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>
     <system.serviceModel>
         <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IAuthentication" />
            </basicHttpBinding>
         </bindings>
         <client>
            <endpoint address="http://localhost/SecurityServices/Authentication.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAuthentication" contract="SecurityService.IAuthentication" name="BasicHttpBinding_IAuthentication" />
         </client>
      </system.serviceModel>
      <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>
   </configuration>

当执行到DatabaseInitializationUtilities尝试运行脚本的代码行时

context.Database.ExecuteSqlCommand(script.ScriptText)

错误信息:

没有找到与固定名称 'System.Data.SqlClient' 的 ADO.NET 提供程序匹配的 Entity Framework 提供程序。请确保在应用程序配置文件的 'entityFramework' 部分中注册了提供程序。有关更多信息,请参见http://go.microsoft.com/fwlink/?LinkId=260882

我相信解决方法就是我配置文件中的内容,所以我不理解这个问题。

注意:Resharper 在节点上标出蓝线并报告“元素 'EntityFramework' 具有无效的子元素 'providers'。然而,当我安装 EF6 时,该部分被 NuGet 注入。

有什么想法吗?


请看下面的问题:https://dev59.com/f2Yr5IYBdhLWcg3wAFg0 - Lloyd
谢谢。对于https://dev59.com/f2Yr5IYBdhLWcg3wAFg0的评论很有帮助。 - Dewey
1
将 EntityFramework.SqlServer.dll 添加到客户端应用程序中,并在其 app.config 文件中添加连接字符串即可解决问题。现在错误已经消失了。https://dev59.com/CGMl5IYBdhLWcg3wXWET - Junior Mayhé
在我们的情况下,EF程序集被GAC注册。在这种情况下,您应该在配置文件中使用完整的程序集名称:<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> - pholpar
10个回答

53

你需要创建一个引用,这样它就会被复制到 debug 文件夹中。稍后可以在运行时访问它。

不需要复制任何文件,只需创建此引用:


private volatile Type _dependency;

public MyClass()
{
    _dependency = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
}

6
史诗级胜利!它失败的原因是只有直接使用的程序集才会被复制。由于该程序集没有直接引用,因此不会被复制。 - satnhak
11
我认为EF团队需要对这件事做出大量解释。 - ProfK
下载dll文件并手动添加到主应用程序中。确保在属性下“复制本地= true”。我不建议使用解决方案来回答该问题,因为当您更新实体模型时,临时代码将被删除。 - DJJ
那真的很有趣。不确定它是如何工作的,但它确实有效 :-) - AH.

17

您的应用程序路径中是否有assembly EntityFramework.SqlServer.dll? 我曾经遇到过同样的问题,将dll复制到应用程序路径后,一切都正常了。


我遇到了同样的问题,并通过添加缺失的EntityFramework.SqlServer.dll文件来解决它。 - Channa

13

是的,这也是我的问题... 我删除了整个数据库并从头开始检查解决方案,在第一次构建后,我得到了这个确切的“错误消息” :-) 插入易失性引用,它就可以工作了。 - Xan-Kun Clark-Davis
那么为什么不在这里简要提及它的主要思想呢? - Deilan
@Deilan,这个想法与dh_cgn的答案中的想法是一样的。https://dev59.com/0mEi5IYBdhLWcg3wjczE#21531672 - Michael Freidgeim

5

我也遇到了这个问题。当我运行我的 .net mvc 应用程序时,一切都在本地工作,但是当我发布它时,我得到了这个错误。问题是,我必须在我的 Web 项目中引用 EntityFramework.SqlServer,尽管我有一个独立的 Data 项目。奇怪的是,只有在发布应用程序时才会抛出此错误。这可能是微软的一个严重 bug。我使用的是 EF 6.1.1。


我曾经遇到过完全相同的问题。我的 ef 程序集引用在一个单独的数据项目中,该项目被我的 mvc 项目引用。我从 nuget 安装了 ef 到我的 mvc 项目中,重新发布后问题得到解决。感谢修复。 - Mike

4
这是因为EntityFramework.SqlServer.dll没有复制到您的项目中。添加该dll,它就有希望工作了。您可以从添加数据模型的项目中找到它。

2
对我来说,问题在于web.config文件中我的提供程序引用不正确。我认为nuget包可能没有正确地包含该提供程序。
我将我的提供程序替换为以下内容,并且它可以工作:
  <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>

2
我也曾遇到类似的问题。 我的问题通过以下方法得到了解决: 图片描述在此输入 图片描述在此输入

0
解决这种问题的最佳方法是将引用EntityFramework.SqlServer.dll包含到您的项目中,然后按F4键(或打开属性)更改属性为Copy to Local = True,这样每次发布应用程序时它都会被复制到发布文件夹中。

0
将 "EntityFramework.SqlServer.dll" 添加到您的项目 bin 文件夹中,即可解决您的问题。

0

我倾向于将EntityFramework添加到Web项目中,以及实际引用它的程序集。


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