执行dotnet ef脚手架命令时遇'无法检索项目元数据'错误
问题:dotnet ef脚手架命令及dotnet build版本冲突问题
项目路径:C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj
执行以下dotnet ef命令时出现错误:
dotnet ef dbcontext scaffold "<well-formed connection string>" Microsoft.EntityFrameworkCore.SqlServer --data-annotations --use-database-names --output-dir Entities --namespace App.Core.DataLayer.Entities --context-namespace App.Core.DataLayer
错误信息:
C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj : error : Could not resolve SDK "Microsoft.NET.Sdk". Exactly one of the probing messages below indicates why we could not resolve the SDK. Investigate and resolve that message to correctly specify the SDK. C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj : error : SDK resolver "Microsoft.DotNet.MSBuildWorkloadSdkResolver" returned null. C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj : error : The NuGetSdkResolver did not resolve this SDK because there was no version specified in the project or global.json. C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj : error : MSB4276: The default SDK resolver failed to resolve SDK "Microsoft.NET.Sdk" because directory "C:\Program Files\dotnet\sdk\7.0.302\Sdks\Microsoft.NET.Sdk\Sdk" did not exist. C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj : error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found. Unable to retrieve project metadata. Ensure it's an SDK-style project. If you're using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.
已尝试操作:
- 修复.NET 8.0 SDK
- 在项目根目录配置指定8.0.101版本的global.json
当前环境信息:执行dotnet --version返回8.0.101
2024年1月26日更新
按照错误提示安装.NET 7.0.302版本后,重新执行命令出现构建失败,执行dotnet build时的错误信息:
MSBuild version 17.8.3+195e7f5a3 for .NET Determining projects to restore... C:\Program Files\dotnet\sdk\7.0.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(151,5): error NETSDK1045: The current .NET SDK does not support targeting .NET 8.0. Either target .NET 7.0 or lower, or use a version of the .NET SDK that supports .NET 8.0. [C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj] Build FAILED. C:\Program Files\dotnet\sdk\7.0.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(151,5): error NETSDK1045: The current .NET SDK does not support targeting .NET 8.0. Either target .NET 7.0 or lower, or use a version of the .NET SDK that supports .NET 8.0. [C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj] 0 Warning(s) 1 Error(s) Time Elapsed 00:00:00.54
已安装的SDK版本(执行dotnet --list-sdks输出):
1.1.14 [C:\Program Files\dotnet\sdk] 6.0.418 [C:\Program Files\dotnet\sdk] 7.0.302 [C:\Program Files\dotnet\sdk] 8.0.101 [C:\Program Files\dotnet\sdk]
系统环境PATH变量已包含C:\Program Files\dotnet,但dotnet build仍默认使用7.0.302版本,添加指定8.0.101版本的global.json后问题依旧,寻求解决办法。
内容的提问来源于stack exchange,提问作者Jason Ayer
相关产品推荐
相关产品推荐

