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

发布ASP.NET WebApi时Entity Framework Migrations无法识别且dotnet-ef安装失败

解决ASP.NET WebApi发布时dotnet-ef安装失败(工具包无法还原)的问题

具体排查与解决步骤

  • 确认.NET SDK版本匹配
    你安装的dotnet-ef版本为7.0.14,需确保本地已安装对应版本的.NET SDK(7.x系列,优先7.0.14)。打开命令行执行:

    dotnet --list-sdks
    

    若未找到对应版本,下载安装同版本SDK,dotnet-ef版本必须与项目依赖的.NET SDK版本一致。

  • 检查并修复NuGet源配置
    网络或源配置错误会导致还原失败。先查看当前NuGet源:

    dotnet nuget list source
    

    若无官方NuGet源,添加官方源:

    dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org
    

    若存在私有源,确认地址可访问,必要时临时禁用其他源,仅保留nuget.org测试。

  • 清理本地NuGet缓存
    缓存损坏可能引发问题,清理后重新安装:

    dotnet nuget locals all --clear
    

    再执行带版本和源的全局安装命令:

    dotnet tool install --global dotnet-ef --version 7.0.14 --source https://api.nuget.org/v3/index.json
    
  • 核对项目EF包引用版本
    Code First场景下,项目需引用与dotnet-ef同版本的EF Core包。打开项目.csproj文件,确认以下引用(以SQL Server为例):

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

    版本不匹配则更新后重新生成项目。

  • 尝试安装为项目本地工具
    全局安装失败时,可将dotnet-ef安装为项目本地工具:

    1. 进入项目根目录执行:
      dotnet new tool-manifest
      
    2. 安装本地工具:
      dotnet tool install dotnet-ef --version 7.0.14
      

    后续使用dotnet ef命令会自动调用本地版本。

  • 切换至Visual Studio包管理器控制台操作
    若命令行环境存在问题,尝试在Visual Studio的Package Manager Console(PMC)中执行:

    Install-Package Microsoft.EntityFrameworkCore.Tools -Version 7.0.14
    

    之后直接使用PMC的迁移命令(如Add-Migration、Update-Database)完成操作。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 08:43:19