具有不变名称“System.Data.SqlClient;"的 ADO.NET 提供程序找不到(实体框架 MVC)

ADO.NET provider with invariant name #39;System.Data.SqlClient;#39; cannot be found (Entity Framework MVC)(具有不变名称“System.Data.SqlClient;的 ADO.NET 提供程序找不到(实体框架 MVC))

本文介绍了具有不变名称“System.Data.SqlClient;"的 ADO.NET 提供程序找不到(实体框架 MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我尝试了以下修复:

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

2) 添加了

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

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

网络配置

 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/><!-- 有关实体框架配置的更多信息,请访问 http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections><应用设置><添加键=网页:版本"值=3.0.0.0"/><添加键=网页:启用"值=假"/><添加键="ClientValidationEnabled" 值="true"/><添加键="UnobtrusiveJavaScriptEnabled" 值="true"/></app设置><连接字符串></处理程序></system.webServer><运行时><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><依赖程序集><assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35"/><bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/></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><依赖程序集><assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35"/><bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/></dependentAssembly><依赖程序集><assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35"/><bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/></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><依赖程序集><assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/><bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/></dependentAssembly><依赖程序集><assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/><bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0"/></dependentAssembly><依赖程序集><assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/><bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/></dependentAssembly><依赖程序集><assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/><bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/></dependentAssembly><依赖程序集><assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/><bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/></dependentAssembly></assemblyBinding></运行时><实体框架><defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/><提供者><provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/></提供者><上下文><context type="ProjectName.Models.ReadContext, ProjectName"><databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[ProjectName.Models.ReadContext, ProjectName], [ProjectName.Migrations.Configuration, ProjectName]], EntityFramework"/></上下文></上下文></实体框架></配置>

解决方案

你有这个:

应该是这样的:

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

I cannot seem to resolve what appears to be a common issue with Entity Framework 6. I have reviewed the many topics related to this issue on SO, and am unable to find a solution that works for my particular case.

I have up until this point been developing using localdb with code first migrations, which worked fine. But now that I am moving this to a actual SQL server instance it throws the following error while attempting migrations:

"The ADO.NET provider with invariant name 'System.Data.SqlClient;' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details. at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)"

I have attempted the following fixes:

1) Uninstalled and Reinstalled Entity Framework

2) Added the code and used :

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;
    }
}

as the base class for my DbSet classes.

3) Added everything to a fresh solution and received the same error.

4) Removed the writecontext and tried migrations with just read, still failed.

5) Attempted several rewrites using information from MSDN

6) I have confirmed that EntityFramework.SqlServer.dll is in my bin directory. It is also referenced properly. Plus I have only one project under my solution.

7) Confirmed I am not using the WebConfig under views.

8) Confirmed I have System.Data dll in bin folder.

Clean/Rebuilding and deletion of existing migrations folder done after each of the above steps.

I am at a loss to explain why I still am receiving this error.

Web Config

    <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>

解决方案

You have this:

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

It should be this:

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

Otherwise, you're telling ADO to find a provider named System.Data.SqlClient; - there isn't one.

这篇关于具有不变名称“System.Data.SqlClient;"的 ADO.NET 提供程序找不到(实体框架 MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:具有不变名称“System.Data.SqlClient;"的 ADO.NET 提供程序找不到(实体框架 MVC)

基础教程推荐