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

Dew*_*wey 36 .net c# sql-server asp.net entity-framework-6

我们正在使用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>
Run Code Online (Sandbox Code Playgroud)

执行到达DatabaseInitializationUtilities尝试运行脚本的行时

context.Database.ExecuteSqlCommand(script.ScriptText)
Run Code Online (Sandbox Code Playgroud)

抛出错误:

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

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

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

有任何想法吗?

dhc*_*cgn 52

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

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

private volatile Type _dependency;

public MyClass()
{
    _dependency = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为EF团队在这一方面有一些重要的解释. (11认同)
  • 大胜!失败的原因是只有直接使用的程序集才会被复制.此程序集没有直接引用,因此不会被复制. (6认同)

小智 17

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


The*_*tor 5

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