迁移至.NET Core 3.0后Azure DevOps Pipeline NuGet还原失败求助
这个问题的核心原因很明确:你使用的NuGet版本(4.9.1)不支持识别.NET Core 3.0与.NET Standard 2.1之间的兼容性。
NuGet 4.9.1是2019年初发布的版本,而.NET Core 3.0和.NET Standard 2.1是在2019年下半年推出的。旧版本的NuGet无法理解.NETCoreApp 3.0完全兼容.NETStandard 2.1这一点,所以错误地判定EF Core 3.0.0包(目标是netstandard2.1)与你的netcoreapp3.0项目不兼容。
解决方案步骤:
升级NuGet工具版本
修改Pipeline中的NuGetToolInstaller@1任务,将版本指定为支持.NET Core 3.0的NuGet 5.x版本(推荐5.0及以上,比如5.8.1或者直接使用最新稳定版)。修改后的任务示例:
- task: NuGetToolInstaller@1 displayName: 'Use NuGet 5.8.1' inputs: versionSpec: 5.8.1 checkLatest: true可选:合并Restore步骤(简化Pipeline)
实际上,.NET Core 3.0 SDK中的dotnet restore命令已经可以处理所有类型的项目(包括.NET Framework 4.8),只要NuGet版本足够新。你可以尝试移除单独的NuGetCommand@2任务,只用DotNetCoreCLI@2的restore步骤来处理所有项目,这样能减少步骤并避免工具版本不一致的问题。修改后的restore部分:
- task: UseDotNet@2 displayName: 'Use .NET Core SDK 3.0' inputs: version: 3.0.100 - task: NuGetToolInstaller@1 displayName: 'Use NuGet 5.8.1' inputs: versionSpec: 5.8.1 checkLatest: true - task: DotNetCoreCLI@2 displayName: 'dotnet restore for all projects' inputs: command: restore projects: '**/*.csproj'验证项目目标框架
确保你的.NET Core 3.0项目的.csproj文件中目标框架是正确的:<TargetFramework>netcoreapp3.0</TargetFramework>同时确认EF Core包的版本确实是3.0.0(与.NET Core版本匹配)。
为什么这样能解决问题?
NuGet 5.0及以上版本已经更新了兼容性规则,明确知道.NETCoreApp 3.0兼容.NETStandard 2.1,所以不会再误报NU1202错误。升级后,NuGet就能正确识别EF Core 3.0.0包与你的netcoreapp3.0项目是兼容的。
内容的提问来源于stack exchange,提问作者Luis Abreu

