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

Ubuntu 22.04下.NET 6中dotnet ef migrations命令无法识别求助

.NET 6 Ubuntu 22.04下dotnet ef migrations命令执行失败的解决步骤
  • 确认EF Core全局工具版本匹配
    Ubuntu下默认可能未安装对应版本的EF Core工具,执行以下命令安装或更新到.NET 6兼容的版本:

    dotnet tool install --global dotnet-ef --version 6.*
    # 若已安装则更新
    dotnet tool update --global dotnet-ef --version 6.*
    

    同时检查全局工具路径是否加入系统环境变量:
    执行echo $PATH,若未包含~/.dotnet/tools,编辑~/.bashrc(或~/.zshrc,取决于你用的shell),添加:

    export PATH="$PATH:$HOME/.dotnet/tools"
    

    执行source ~/.bashrc使配置生效。

  • 验证项目EF Core包版本一致性
    确保项目中所有EF Core相关NuGet包版本与.NET 6完全匹配(必须为6.x系列),打开项目的.csproj文件,检查如下引用:

    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.26" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.26">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    

    版本号需统一,避免混合不同大版本的包。

  • 确保命令执行路径正确
    必须在包含DbContext类的项目目录下执行迁移命令,若在解决方案根目录执行,需通过--project参数指定目标项目路径:

    dotnet ef migrations add InitialCreate --project ./YourTargetProjectFolder
    
  • 安装系统依赖库
    .NET 6在Ubuntu 22.04下依赖libicu-dev库,缺失会导致EF工具执行异常,执行以下命令安装:

    sudo apt-get update && sudo apt-get install libicu-dev -y
    
  • 清理项目缓存并重建
    清理项目缓存后重新构建,排除编译缓存导致的问题:

    dotnet clean
    dotnet restore
    dotnet build
    

    完成后再次尝试执行迁移命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 18:20:03