Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix: Add a reference to ".NETFramework,Version=v4.7.1" in the "TargetFrameworks" property of your project file and then re-run NuGet restore?

And yes, I have tried all similar QAs and nothing helped.

What I have tried, none helped:

  1. Delete bin/ & obj/
  2. Delete project.assets.json
  3. Delete .vs/
  4. Reset entire git repo
  5. Clean nuget caches
  6. Change .NetFramework version

What I know:

  1. This happened to my colleague month ago on Win7, VS19 15.x, update did nothing. Thought she was crazy and fucked up something in system. Now happened to me, Win10, VS19, all up to date.
  2. Yesterday VS seamed slow, so I have deleted .vs, could be related.
  3. I have isolated first affected project, it is pure c# .net framework 4.7.1 lib, no other dependencies than system .net libs, does not have any nuget reference.
  4. After all that deleting and reseting one thing helped. I have renamed DotNetExtensions.csproj to DotNetExtensions1.csproj and now shit builds like a charm, but when I rename it back same error again.

WTF? How is this possible? What else can i clean?

Full log:

Rebuild started...
1>------ Rebuild All started: Project: DotNetExtensions, Configuration: Release Any CPU ------
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\NuGet\16.0\Microsoft.NuGet.targets(198,5): error : Your project does not reference ".NETFramework,Version=v4.7.1" framework. Add a reference to ".NETFramework,Version=v4.7.1" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
like image 478
watbywbarif Avatar asked Apr 13 '21 09:04

watbywbarif


2 Answers

I tried to port my projects to .net 5.0 and encountered this problem when reverting back to 4.8. This worked for me:

  1. Clean project
  2. Delete bin and obj folders
  3. Builds again.
like image 73
Gregory Liénard Avatar answered Sep 18 '22 14:09

Gregory Liénard


I had a very similar error except it was:

Your project does not reference ".NETFramework,Version=v4.8" framework. Add a reference to ".NETFramework,Version=v4.8" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.

What worked for me was to use Visual Studio to 1) clean the solution and then 2) delete all obj and bin folders. Then it built without restarting VS.

I think it was a bad cache of the project.assets.json file. It must have gotten corrupt when restoring a stash that required a merge. VS didn't automatically load the projects. It isn't related to the TargetFrameworks because when you add that property to the csproj file, a different build step fails.

like image 37
philipwolfe Avatar answered Sep 18 '22 14:09

philipwolfe