You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC(.NET4.6.2)部署Azure后仍用System.Data.SqlClient而非新组件

问题分析与解决方案

我维护的基于.NET 4.6.2和Entity Framework的旧ASP.NET MVC应用,已将SQL提供者从System.Data.SqlClient切换为Microsoft.Data.SqlClient,本地运行正常,但部署到Azure后仍使用旧提供者,导致连接字符串的Authentication参数解析失败。

可能的原因及解决步骤

1. Azure应用服务的.NET框架依赖优先级问题

Azure应用服务可能默认加载系统版本的System.Data.SqlClient,而非部署包中的NuGet版本。

  • 解决:在项目web.config中添加绑定重定向,强制使用Microsoft.Data.SqlClient:
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Data.SqlClient" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.6.1.0" newVersion="4.6.1.0" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.Data.SqlClient" publicKeyToken="23ec7fc2d6eaa4a5" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-[你的包版本号]" newVersion="[你的包版本号]" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

2. Entity Framework未配置使用新提供者

旧版EF(如EF6)默认绑定System.Data.SqlClient,需显式指定新提供者。

  • 解决:在web.config的entityFramework节点更新配置:
<entityFramework>
  <providers>
    <provider invariantName="Microsoft.Data.SqlClient" type="Microsoft.Data.SqlClient.SqlProviderServices, Microsoft.Data.SqlClient.EntityFrameworkCore" />
    <!-- 注释或移除旧的System.Data.SqlClient提供者配置 -->
    <!-- <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> -->
  </providers>
</entityFramework>

同时确保连接字符串的providerName设置为Microsoft.Data.SqlClient:

<connectionStrings>
  <add name="YourDbContext" connectionString="Server=tcp:...;Authentication=Active Directory Integrated;..." providerName="Microsoft.Data.SqlClient" />
</connectionStrings>

3. Azure部署包缺少依赖文件

部署时NuGet包可能未被正确包含到Azure的部署文件中。

  • 解决:
    • 确认项目中Microsoft.Data.SqlClient及对应EF适配包的Copy Local属性设为True;
    • 清理本地解决方案,重新生成后再部署;
    • 登录Azure应用服务的Kudu控制台,检查wwwroot/bin目录是否存在Microsoft.Data.SqlClient.dll。

4. Azure应用服务运行时环境不匹配

应用服务配置的.NET版本与本地不一致,可能导致依赖加载异常。

  • 解决:
    • 登录Azure门户,进入应用服务的「配置」>「常规设置」;
    • 确认「.NET版本」设置为.NET Framework 4.6.2(与本地一致);
    • 重启应用服务后测试。

验证方法

部署后可添加诊断代码,确认当前使用的SqlClient程序集:

var assembly = typeof(Microsoft.Data.SqlClient.SqlConnection).Assembly;
Response.Write($"当前SqlClient程序集:{assembly.FullName}");

内容的提问来源于stack exchange,提问作者haagel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 16:10:11