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

GitHub Actions中Entity Framework Core迁移失败问题排查

解决方案:EF Core迁移包登录Azure SQL失败(用户为空)

针对你遇到的Login failed for user ''错误,结合本地正常、GitHub Actions中连接串已传递的情况,可按以下步骤排查解决:

1. 修复Shell解析导致的连接串截断/转义问题

GitHub Actions的Linux runner使用bash执行命令,若连接串包含特殊字符(如$、!、@),会被shell解析为变量或特殊符号,导致用户名/密码丢失。需用单引号包裹连接串避免解析:

- name: Run EF Migrations Bundle
  run: ./efbundle --connection '${{ steps.deploy-resources.outputs.sqlServerConnectionString }}'

2. 验证连接串的完整性

在执行迁移前添加调试步骤,确认连接串包含完整的用户名和密码(注意不要直接打印敏感内容,可只检查格式):

- name: Validate Connection String Format
  run: |
    conn_str="${{ steps.deploy-resources.outputs.sqlServerConnectionString }}"
    echo "Connection string contains User ID: $(echo $conn_str | grep -q 'User ID=' && echo 'Yes' || echo 'No')"
    echo "Connection string contains Password: $(echo $conn_str | grep -q 'Password=' && echo 'Yes' || echo 'No')"

3. 生成自包含的迁移包

GitHub Actions runner的环境可能缺少.NET运行时依赖,导致迁移包运行时解析连接串异常。生成迁移包时指定自包含参数和目标运行时:

dotnet-ef migrations bundle --project src/KeenDev.BlogPlatform.Infrastructure/KeenDev.BlogPlatform.Infrastructure.csproj --startup-project src/KeenDev.BlogPlatform.API/KeenDev.BlogPlatform.API.csproj --output efbundle --self-contained true --runtime linux-x64

4. 检查Azure SQL防火墙规则

确认GitHub Actions runner的IP已加入SQL Server防火墙允许列表,或临时允许Azure服务访问(测试用):

在你的SQL Server Bicep中添加防火墙规则:

resource sqlFirewallRule 'Microsoft.Sql/servers/firewallRules@2022-05-01-preview' = {
  parent: sqlServer
  name: 'AllowAzureServices'
  properties: {
    startIpAddress: '0.0.0.0'
    endIpAddress: '0.0.0.0'
  }
}

5. 确认连接串格式正确

确保连接串为SQL账号认证的标准格式,无多余的Entra ID相关参数:

Server=tcp:{serverName}.database.windows.net,1433;Initial Catalog={dbName};Persist Security Info=False;User ID={adminLogin};Password={adminPassword};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:50:08