ADO.NET提供程序不变名'System.Data.SqlClient;'找不到(实体框架MVC)

4

我似乎无法解决一个在Entity Framework 6中普遍存在的问题。我已经在SO上查看了许多与此问题相关的主题,但是对于我的特定情况,我无法找到有效的解决方案。

到目前为止,我一直使用本地db和Code First迁移进行开发,这很好。但是现在,当我将其移动到实际SQL服务器实例时,它在尝试迁移时会抛出以下错误:

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

我尝试了以下修复方法:

1)卸载并重新安装Entity Framework

2)添加代码并使用:

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并尝试只使用read进行迁移,仍然失败。
5) 尝试使用MSDN中的信息进行多次重写。
6) 我确认EntityFramework.SqlServer.dll在我的bin目录中。它也被正确引用。此外,我的解决方案中只有一个项目。
7) 确认我没有在views的WebConfig下使用。
8) 确认我在bin文件夹中有System.Data dll。

bin文件夹的图像

每个以上步骤后都完成了清理/重新构建和删除现有的迁移文件夹。
我不知道为什么我仍然收到这个错误。
Web配置
    <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 - jsanalytics
我添加了更多信息,请查看bin文件夹的图像。正在复制。 - Rasen244
是的,那确实是问题所在。 - Rasen244
3个回答

14

你有这个:

<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 的提供程序 - 实际上是不存在的。

否则,你告诉ADO去寻找一个名为 System.Data.SqlClient 的提供程序 - 实际上是不存在的。


1

我遇到了这个错误,但对我来说,它完全是另一回事。通过仅查看 无法找到所请求的 .Net Framework 数据提供程序找到此答案,我成功地解决了它。

然后,我不得不编辑:

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>

当我去掉末尾的<DbProviderFactories />之后,一切又开始正常工作了。


-2

对我来说,这个问题是由于 EntityFramework 受损引起的。卸载 EntityFramework 以及 Visual Studio 并重新安装 Visual Studio - Visual Studio 安装包含了 .Net Framework SDK 的安装,这样就可以解决这个问题。


1
不过事实上,问题就在于我加了一个分号。尽管在别人指出这个笔误之前,我也试过你的解决方案。 - Rasen244

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接