【问题标题】:ADO.NET provider with invariant name 'System.Data.SqlClient;' cannot be found (Entity Framework MVC)具有不变名称“System.Data.SqlClient;”的 ADO.NET 提供程序找不到(实体框架 MVC)
【发布时间】:2015-09-05 17:48:15
【问题描述】:

我似乎无法解决 Entity Framework 6 的常见问题。我在 SO 上查看了与此问题相关的许多主题,但无法找到适合我的特定情况的解决方案。

到目前为止,我一直在使用带有代码优先迁移的 localdb 进行开发,效果很好。但是现在我将它移动到一个实际的 SQL 服务器实例,它在尝试迁移时会引发以下错误:

"具有不变名称 'System.Data.SqlClient;' 的 ADO.NET 提供程序要么未在机器或应用程序配置文件中注册,要么无法加载。有关详细信息,请参阅内部异常。 在 System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)"

我尝试了以下修复:

1) 卸载并重新安装实体框架

2) 添加code 并使用:

public abstract class BaseDomainContext : DbContext
{
    static BaseDomainContext()
    {
        // ROLA - This is a hack to ensure that Entity Framework SQL Provider is copied across to the output folder.
        // As it is installed in the GAC, Copy Local does not work. It is required for probing.
        // Fixed "Provider not loaded" error
        var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
    }
}

作为我的 DbSet 类的基类。

3) 将所有内容添加到新的解决方案中并收到相同的错误。

4) 删除了 writecontext 并尝试使用 just read 进行迁移,但仍然失败。

5) 尝试使用来自MSDN 的信息进行多次重写

6) 我已经确认 EntityFramework.SqlServer.dll 在我的 bin 目录中。它也被正确引用。另外,我的解决方案下只有一个项目。

7) 确认我没有使用视图下的 WebConfig。

8) 确认我在 bin 文件夹中有 System.Data dll。

在上述每个步骤之后,清理/重建和删除现有迁移文件夹。

我不知道为什么我仍然收到此错误。

网络配置

    <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 --></configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <connectionStrings>

    <add name="ReadContext" connectionString="Server=Sql2014intsnace;Database=database;User Id=secret; Password=secret;" providerName="System.Data.SqlClient;" />
    <add name="WriteContext" connectionString="Server=Sql2014intsnace;Database=database;User Id=secret; Password=secret;" providerName="System.Data.SqlClient;" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Windows" />
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>
  <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />    
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    <contexts> 
  <context type="ProjectName.Models.ReadContext, ProjectName"> 
    <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[ProjectName.Models.ReadContext, ProjectName], [ProjectName.Migrations.Configuration, ProjectName]], EntityFramework" /> 
  </context> 
</contexts>
  </entityFramework>
</configuration>

【问题讨论】:

  • 听起来你不见了System.Data.dll
  • 我添加了更多信息,请参见 bin 文件夹的图像。它正在复制。
  • 是的,这确实是问题所在。

标签: asp.net .net asp.net-mvc entity-framework entity-framework-6


【解决方案1】:

你有这个:

<add name="ReadContext" 
     connectionString="Server=Sql2014intsnace;Database=database;User Id=secret; Password=secret;" 
     providerName="System.Data.SqlClient;" /> <-- semi-colon

应该是这样的:

<add name="ReadContext" 
     connectionString="Server=Sql2014intsnace;Database=database;User Id=secret; Password=secret;" 
     providerName="System.Data.SqlClient" /> <-- no semi-colon

否则,您是在告诉 ADO 查找名为 System.Data.SqlClient; 的提供程序 - 没有。

【讨论】:

    【解决方案2】:

    我收到了这个错误,但对我来说,这是完全不同的事情。我只能通过查看 Unable to find the requested .Net Framework Data Providerfinding this answer 来解决它。

    然后我不得不编辑:

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config
    

    还有:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
    

    搜索DbProviderFactories 两个配置看起来像这样:

    <system.data>
        <DbProviderFactories>
            <add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26" />
        </DbProviderFactories>
        <DbProviderFactories />
    </system.data>
    

    当我删除尾随的 &lt;DbProviderFactories /&gt; 后,一切又开始工作了。

    【讨论】:

      【解决方案3】:

      对我来说,这个问题是由损坏的 EntityFramework 引起的。卸载 EntityFramework 以及 Visual Studio 并重新安装 Visual Studio - Visual Studio 安装涵盖了 .Net Framework SDK 安装类型解决了这个问题。

      【讨论】:

      • 不,实际上是我包含了一个分号。尽管在向我指出错字之前我确实尝试过您的解决方案。
      猜你喜欢
      • 2014-02-06
      • 2013-08-29
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      相关资源
      最近更新 更多