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

EF6迁移至Azure:替换Microsoft.Data.SqlClient遇错求助

问题描述

我们正将本地数据库迁移至Azure,现有应用基于Entity Framework 6开发。此前使用System.Data.SqlClient搭配Microsoft Active Directory Password认证,安装ADALSQL.dll后可正常运行;未安装该DLL时,异常消息中指向的链接返回404错误(该链接此前可正常访问)。

由于ADALSQL已被弃用,我们尝试将应用切换为使用Microsoft.Data.SqlClient,已执行以下操作:

  • 向项目添加NuGet包Microsoft.Data.SqlClient
  • 在app.config中添加如下配置:
<system.data>
    <DbProviderFactories>
        <add name="Microsoft SqlClient Data Provider" 
             invariant="Microsoft.Data.SqlClient" 
             description="Microsoft SqlClient Data Provider for SQL Server" 
             type="Microsoft.Data.SqlClient.SqlClientFactory, Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5" />
    </DbProviderFactories>
</system.data>
  • 更新app.config中EntityFramework节点的providers部分:
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <!--<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
        <provider invariantName="Microsoft.Data.SqlClient"  
                  type="Microsoft.Data.SqlClient.SqlClientFactory, Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5" />
    </providers>
</entityFramework>
  • 更新连接字符串以引用Microsoft.Data.SqlClient:
<add name="B2BEntities" 
     connectionString="metadata=res://*/Models.B2B.csdl|res://*/Models.B2B.ssdl|res://*/Models.B2B.msl;provider=Microsoft.Data.SqlClient;provider connection string='data source=

运行应用时抛出如下异常:

The 'Instance' member of the Entity Framework provider type 'Microsoft.Data.SqlClient.SqlClientFactory, Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance of the provider. This may be because the provider does not support Entity Framework 6 or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

解决方法

1. 安装EF6专属的Microsoft.Data.SqlClient适配包

当前使用的Microsoft.Data.SqlClient仅提供基础数据访问能力,缺少EF6所需的Provider实现。需安装Microsoft.Data.SqlClient.EntityFramework6 NuGet包,这是微软官方为EF6适配Microsoft.Data.SqlClient提供的专属组件。

2. 修正Entity Framework Provider配置

EF6的Provider必须指向SqlProviderServices实现类,而非SqlClientFactory。将app.config中entityFramework节点下的Provider配置修改为:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <!-- 若仍需使用System.Data.SqlClient,可取消注释;否则保留注释 -->
        <!--<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
        <provider invariantName="Microsoft.Data.SqlClient"  
                  type="Microsoft.Data.SqlClient.EntityFramework6.SqlProviderServices, Microsoft.Data.SqlClient.EntityFramework6" />
    </providers>
</entityFramework>

3. 完善连接字符串

补充完整Azure SQL的AD密码认证参数,Microsoft.Data.SqlClient原生支持该认证方式,无需依赖ADALSQL.dll:

<add name="B2BEntities" 
     connectionString="metadata=res://*/Models.B2B.csdl|res://*/Models.B2B.ssdl|res://*/Models.B2B.msl;provider=Microsoft.Data.SqlClient;provider connection string='data source=你的Azure SQL服务器地址.database.windows.net;initial catalog=你的数据库名;user id=你的AD账号@域名.com;password=你的AD密码;authentication=Active Directory Password;'" />

4. 验证依赖一致性

确保DbProviderFactories配置中的版本号与安装的Microsoft.Data.SqlClient包版本一致;若使用最新包,可省略Version参数以避免版本冲突。

5. 清理旧依赖

移除项目中ADALSQL.dll相关的引用或安装项,确保应用不再依赖已弃用的组件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 00:50:21