Jason Ayer
Jason Ayer

Reputation: 833

Getting error "Unable to retrieve project metadata." when running dotnet ef dbcontext scaffold command

My project at C:\Source\local\EntityFrameworkCore\EntityFrameworkCore\EntityFrameworkCore.csproj is giving me the error:

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.

When I run the command:

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

I have tried repairing my dotnet8.0 sdk and it has made no difference. I tried specifiying version 8.0.101 in a global.json file in the project root, no difference. I'm not sure what else to try now. I see its talking about being unable to locate a specific version of dotnet in the error, but I don't see a reference to that anywhere in my system environment PATH variable.

Running dotnet --version gives me 8.0.101. I'm out of ideas.

UPDATE 1/26/2024:

I installed .NET version 7.0.302 as indicated in the error message, and when I rerun the command it's indicating a failure to build. When I run dotnet build this is the error I get now:

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

Running dotnet --list-sdks shows me this:

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]

My environment has this in the PATH variable:

C:\Program Files\dotnet

So I'm not sure why dotnet build is trying to use version 7.0.302 specifically. Adding a global.json to my project with the version 8.0.101 makes no difference in result, I get the same error as before.

Upvotes: 1

Views: 872

Answers (1)

Dave H
Dave H

Reputation: 11

Install "Build tools for Visual Studio 2022 Preview"

https://visualstudio.microsoft.com/vs/preview/

and then using MSBuild from the path directly:

C:\Program Files (x86)\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin

Upvotes: 1

Related Questions