c# - 未找到具有不变名称 'System.Data.SqlClient' 的 ADO.NET 提供程序的 Entity Framework 提供程序。

标签 c# asp.net .net sql-server 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>

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

context.Database.ExecuteSqlCommand(script.ScriptText)

抛出错误:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

我相信补救措施正是我的配置文件中的内容,所以我不明白这个问题。

注意:Resharper 正在对节点进行蓝线化并报告 “元素‘EntityFramework’具有无效的子元素‘providers’。但是,该部分是在我安装 EF6 时由 NuGet 注入(inject)的。

有什么想法吗?

最佳答案

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

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

private volatile Type _dependency;

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

关于c# - 未找到具有不变名称 'System.Data.SqlClient' 的 ADO.NET 提供程序的 Entity Framework 提供程序。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21175713/

相关文章:

c# - 系统.Data.SqlClient.SqlException : Invalid column name 'phone_types_phone_type_id'

c# - 条件表达式中的数据类型不匹配 | Access 、OleDb、C#

javascript - 使用动态表仅填充数据并将值传递给 ASP.NET 背后的代码

.net - map 和字典的实现差异

c# - 如何正确使用 Task.WhenAll()

c# - 优化 Linq : IN operator with large data

c# - 将参数传递给 ASP.NET Core 中的 jQuery 加载函数

c# - 测试非阻塞队列

具有多选功能的 asp.net mvc 强类型 View 模型

c# - 为什么我不能将属性转换为嵌套元素?