首页 文章

找不到具有不变名称'System.Data.SqlClient'的ADO.NET提供程序的实体框架提供程序 .

提问于
浏览
34

我们正在使用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)

抛出错误:

没有为ADO.NET提供程序找到具有不变名称“System.Data.SqlClient”的实体框架提供程序 . 确保提供程序已在应用程序配置文件的“entityFramework”部分中注册 . 有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882 .

我相信补救措施正是我在配置文件中所拥有的,所以我不明白这个问题 .

注意:Resharper使节点变蓝并报告“元素'EntityFramework'具有无效的子元素'提供者' . 但是,当我安装EF6时,NuGet会注入该部分 .

有任何想法吗?

10 回答

  • 5

    您需要创建一个引用,因此它将被复制到调试文件夹中 . 所以稍后它可以在运行时访问 .

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

    private volatile Type _dependency;
    
    public MyClass()
    {
        _dependency = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
    }
    
  • 0

    你的应用程序路径中有程序集EntityFramework.SqlServer.dll吗?我遇到了同样的问题,在将dll复制到应用程序路径后,每个Thing工作正常 .

  • 50
  • 0

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

  • 2

    这是因为 EntityFramework.SqlServer.dll 未复制到您的项目中 . 添加该DLL,它将有希望工作 . 您可以从添加datamodel的项目中找到它 .

  • 0

    对我来说,结果是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>
    
  • 13

    解决这类问题的最佳方法是将参考EntityFramework.SqlServer.dll包含到您的项目中按F4(操作属性)将属性更改为Copy to Local = True,以便每次发布时应用程序将它复制到已发布的文件夹 .

  • 4

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

  • 17

    我也有类似的问题

    通过执行以下操作解决了我的问题:
    enter image description here

    enter image description here

  • 0

    将“EntityFramework.SqlServer.dll”添加到项目bin文件夹中将解决您的问题 .

相关问题