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

.NET 7项目已安装指定包仍无法使用aspnet-codegenerator代码生成器

.NET 7 Scaffolding 功能失效排查(常规方案无效)

问题背景

  • 项目目标框架:.NET 7
  • 已安装SDK:7.0.203(包含.NET Runtime 7.0.5、ASP.NET Core Runtime 7.0.5)
  • 核心问题:Scaffolding(代码生成)功能突然失效,期间未对项目进行任何更新或升级操作
  • 已尝试的操作:
    • 全局及本地卸载重装Microsoft.VisualStudio.Web.CodeGeneration.Design包与EF Core包
    • 分别测试Microsoft.VisualStudio.Web.CodeGeneration.Design 7.0.6(最新版)和7.0.5(与EF Core版本匹配)
    • 清理NuGet包缓存
    • 删除.vs文件夹

命令行输出

PM>  dotnet aspnet-codegenerator --help
....
No code generators are available in this project. Add Microsoft.VisualStudio.Web.CodeGeneration.Design package to the project as a NuGet package reference.

PM> dotnet list package | select-string Microsoft.VisualStudio.Web.CodeGeneration.Design

   > Microsoft.VisualStudio.Web.CodeGeneration.Design         7.0.5       7.0.5   

可能遗漏的排查步骤

  • 检查.csproj文件的包引用有效性
    手动打开项目的.csproj文件,确认Microsoft.VisualStudio.Web.CodeGeneration.Design的引用没有被条件编译限制(比如仅在特定框架/配置下生效)。标准引用格式如下,若存在Condition属性直接删除:

    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.5">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    
  • 对齐全局工具与项目包版本
    执行dotnet tool list -g查看全局安装的dotnet-aspnet-codegenerator版本,必须和项目使用的包版本(7.0.x)一致。版本不匹配的话,卸载重装对应版本:

    dotnet tool uninstall -g dotnet-aspnet-codegenerator
    dotnet tool install -g dotnet-aspnet-codegenerator --version 7.0.5
    
  • 尝试调用项目本地工具执行
    若全局工具存在版本冲突,直接调用项目本地依赖的工具文件:

    dotnet exec --depsfile .\bin\Debug\net7.0\[你的项目名].deps.json --runtimeconfig .\bin\Debug\net7.0\[你的项目名].runtimeconfig.json C:\Users\[你的用户名]\.nuget\packages\microsoft.visualstudio.web.codegeneration.tools\7.0.5\tools\net7.0\any\dotnet-aspnet-codegenerator.dll --help
    

    注意替换命令中的项目名、用户名及本地包路径。

  • 清理项目输出并重建
    删除项目根目录下的bin和obj文件夹,依次执行dotnet restore、dotnet build,确保项目编译正常后再重试代码生成命令。

  • 锁定项目使用的SDK版本
    执行dotnet --list-sdks查看已安装的SDK版本,若存在多个版本,在项目目录创建global.json文件指定7.0.203:

    {
      "sdk": {
        "version": "7.0.203"
      }
    }
    
  • 检查RuntimeIdentifiers配置
    若.csproj中存在<RuntimeIdentifiers>节点,尝试暂时注释掉,或确保包含当前系统对应的RID(如windows-x64、linux-x64),避免工具无法匹配生成器。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 21:02:38