Why add-migration command is giving error although build succeeds in Visual Studio using .NET

251 Views Asked by At

I'm new to .NET and keep getting the following error when executing add-migration command in Visual Studio 2022. Error Details

You must install or update .NET to run this application.

App: C:\Users\myname\.nuget\packages\microsoft.entityframeworkcore.tools\3.1.1\tools\netcoreapp2.0\any\ef.dll
Architecture: x64
Framework: 'Microsoft.NETCore.App', version '2.0.0' (x64)
.NET location: C:\Program Files\dotnet\

The following frameworks were found:
  3.1.32 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  5.0.17 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  6.0.16 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  7.0.5 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

Learn about framework resolution:
https://aka.ms/dotnet/app-launch-failed

To install missing framework, download:
https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=2.0.0&arch=x64&rid=win10-x64

Notice,
App: C:\Users\myName\.nuget\packages\microsoft.entityframeworkcore.tools\3.1.1\tools\netcoreapp2.0\any\ef.dll does not point to the location of my solution/project but to some nuget package.

.csproj file mentions <TargetFrameworks>netcoreapp3.1</TargetFrameworks>. So I assume my app is using .NET 3.1 which is already installed in my system. But the error says I should have .NET 2.0.0

I tried to check the .NET version in package manager console.
PM> dotnet --version
7.0.203

I genuinely appreciate any fixes or help in understanding why its asking for 2.0.0 when my app uses 3.1 and why its showing 7.0 when I try to check the version in PM Console.

1

There are 1 best solutions below

0
Giorgi Anakidze On
PM> dotnet --version
7.0.203

What this command says is the latest version of .NET you have installed on your system, not what your project uses.

<TargetFrameworks>netcoreapp3.1</TargetFrameworks>

While this already indicates that your app is using .NET Core 3.1, if you want to use .NET 7 you should swap it for

<TargetFramework>net7.0</TargetFramework>

After that I'd update the nuget packages to corresponding versions and try again.