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

使用dotnet ef dbcontext scaffold从Azure SQL生成EF Core模型失败

使用dotnet ef dbcontext scaffold从Azure SQL Database生成EF Core模型时构建失败的排查方案

问题场景

在使用dotnet ef dbcontext scaffold命令从Azure SQL Database生成Entity Framework Core模型时遇到构建失败问题。

连接字符串

Server=[服务器名称].database.windows.net;Database=[数据库名称];User ID=[用户名];Password="[密码]";Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

注:方括号中的内容已替换为实际私密信息

执行的命令

dotnet ef dbcontext scaffold "Server=[服务器名称].database.windows.net;Database=[数据库名称];User ID=[用户名];Password="[密码]";Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -o . --context-dir .

问题现象

执行上述命令后,控制台输出:

Build started...
Build failed. Use dotnet build to see the errors.

但单独运行dotnet build时,仅返回24条警告,无任何错误。

已尝试的操作

  • 运行dotnet build检查构建错误,未发现错误
  • 调整连接字符串和文件夹路径,问题依旧
  • 通过SQL Server Management Studio和Visual Studio的SQL Server对象资源管理器可成功连接目标服务器

环境信息

  • Windows 10
  • Microsoft.EntityFrameworkCore 7.0.13
  • Microsoft.EntityFrameworkCore.Design 7.0.13
  • Microsoft.EntityFrameworkCore.SqlServer 7.0.13
  • Microsoft.EntityFrameworkCore.Tools 7.0.13
  • .NET版本 6.0.400-preview.22330.6

排查与解决建议

  1. 修复命令中的密码引号解析问题
    命令里密码部分用了嵌套双引号(Password="[密码]"),会导致命令行解析错误,试试以下几种调整方式:

    • 用单引号包裹整个连接字符串,内部保留密码的双引号:
      dotnet ef dbcontext scaffold 'Server=[服务器名称].database.windows.net;Database=[数据库名称];User ID=[用户名];Password="[密码]";Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;' Microsoft.EntityFrameworkCore.SqlServer -o . --context-dir .
      
    • 如果密码不含特殊字符,直接去掉密码的双引号:
      dotnet ef dbcontext scaffold "Server=[服务器名称].database.windows.net;Database=[数据库名称];User ID=[用户名];Password=[密码];Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -o . --context-dir .
      
    • 若密码本身包含双引号,用反斜杠转义:
      dotnet ef dbcontext scaffold "Server=[服务器名称].database.windows.net;Database=[数据库名称];User ID=[用户名];Password=\"[密码]\";Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -o . --context-dir .
      
  2. 清理项目构建缓存
    缓存可能导致dotnet ef和dotnet build的构建逻辑不一致,执行以下命令后重试:

    dotnet clean
    dotnet restore
    dotnet build
    dotnet ef dbcontext scaffold [你的连接字符串] Microsoft.EntityFrameworkCore.SqlServer -o . --context-dir .
    
  3. 排查版本兼容性问题
    你用的是EF Core 7.0.13,但.NET SDK是6.0的预览版,预览版可能存在兼容性bug。建议:

    • 把.NET SDK升级到6.0的正式稳定版(比如6.0.425)
    • 或者将EF Core版本降级到6.x系列,与SDK版本匹配
  4. 指定目标项目路径
    如果当前目录下有多个项目,dotnet ef可能选错项目,通过--project参数明确指定:

    dotnet ef dbcontext scaffold "你的连接字符串" Microsoft.EntityFrameworkCore.SqlServer -o . --context-dir . --project ./YourProject.csproj
    
  5. 查看详细日志定位问题
    执行命令时添加-v参数获取详细构建日志,就能看到具体失败原因:

    dotnet ef dbcontext scaffold "你的连接字符串" Microsoft.EntityFrameworkCore.SqlServer -o . --context-dir . -v
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 17:03:13