无法加载文件或程序集“ MySql.Data”或其依赖项之一

时间:2018-07-10 09:49:25

标签: mysql asp.net-mvc entity-framework asp.net-web-api

经过足够的探索,我决定在此处发布此问题。我在WebApi项目中遇到此错误,该项目在本地IIS上运行良好,但是当我在Live Server上部署项目时,遇到此问题无法加载文件或程序集“ MySql.Data”或一个依赖项。我正在使用MySql数据库,并将所有相关的依赖项添加到web.config文件中,但似乎没有任何解决办法。

我的 Web.Config 文件

    <?xml version="1.0" encoding="utf-8"?>
    <!--  
      http://go.microsoft.com/fwlink/?LinkId=301879
      -->
    <configuration>
      <configSections>  
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-IntranetApiDemo-20180709022909.mdf;Initial Catalog=aspnet-IntranetApiDemo-20180709022909;Integrated Security=True" providerName="System.Data.SqlClient" />        
<add name="DBCS" connectionString="Server=14.192.18.129;userid=admin;password=pwd; Database=db; convert zero datetime=True; default command timeout=0; SslMode = none" providerName="System.Data.MySqlClient" />
      </connectionStrings>
      <appSettings></appSettings>
      <system.web>
        <authentication mode="None" />
        <compilation debug="true" targetFramework="4.5.2" />
        <httpRuntime targetFramework="4.5.2" />
      </system.web>
      <system.webServer>
        <modules>
          <remove name="FormsAuthentication" />
        </modules>
        <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.Security" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.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.Optimization" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.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>
          <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="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>  
         <dependentAssembly>
            <assemblyIdentity name="MySql.Data.MySqlClient" publicKeyToken="c5687fc88969c44d" />
            <bindingRedirect oldVersion="1.0.0.0-6.10.7.0" newVersion="6.10.7.0" />
          </dependentAssembly>    
        </assemblyBinding>
      </runtime>
      <entityFramework>
       <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
        <providers>
            <provider invariantName="MySql.Data.MySqlClient"
                type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.7.0"/>
            <provider invariantName="System.Data.SqlClient"
                type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
        </providers>
      </entityFramework>
      <system.codedom>
        <compilers>
          <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
          <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
      </system.codedom>
    </configuration>

如您在我的代码中所见,MySql.Data.Entity.EF6版本的(版本= 6.10.7.0)与MySql.Data.MySqlClient 版本= 6.10匹配。 7.0

Web.Config文件中是否缺少任何内容。 请帮我解决这个问题。

谢谢。

1 个答案:

答案 0 :(得分:0)

听起来,您不仅应该为MySql.Data.MySqlClient命名空间设置程序集绑定重定向,还应该包括其父命名空间,即MySql.Data

<dependentAssembly>
    <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" />
    <bindingRedirect oldVersion="1.0.0.0-6.10.7.0" newVersion="6.10.7.0" />
</dependentAssembly>

注意:在使用此绑定重定向之前,请确保生产服务器中的MySql.Data.dll程序集(及其所有依赖项)存在并且其版本与开发机器中已安装的版本匹配( 6.10.7.0)。

另外,MySQL的连接字符串提供程序应具有MySql.Data.MySqlClient提供程序,如下所示:

<add name="DBCS" connectionString="Server=14.192.18.129;userid=admin;password=pwd; Database=db; convert zero datetime=True; default command timeout=0; SslMode=none" 
     providerName="MySql.Data.MySqlClient" />

参考:

MySQL Entity Framework 6 Support