Your project does not reference ".NETFramework,Version=v4.6.2" framework. Add a reference to ".NETFramework,Version=v4.6.2" in the "TargetFrameworks"

C#Unit TestingMstestCsproj.Net Framework-Version

C# Problem Overview


I can't run my unit tests.

I have the next error:

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

In app.config :

<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>

In Project > Properties > Application > TargetFramework (.NET Framework 4.6.2)

How can I fix it?

C# Solutions


Solution 1 - C#

Please make the next steps

  1. Clean solution
  2. Clean folder "packages"
  3. Delete folder "bin"
  4. Delete folder "obj"

Solution 2 - C#

I experienced similar issue, but with v4.7.2. Namely, I kept getting build log message like this:

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

Despite the fact that it looked similar, none of the above proposed steps worked for me. I kept seeing this message after each build. Nothing seemed to be able to help.

In fact, the problem was related to that, due to migration, I had to put two projects in one folder of code. One of them was targeted at .Net Core, another at .Net Framework, both referenced same .Net Standard libraries. Apparently, they share the same obj folder where Core projects put project.assets.json file. Actually, exactly this file interferres with the Framework project preventing its normal build. Seems even if you performed Migrate from packages.config to PackageReference... which was recommended as one of possible solution.

You can try to fix the issue by putting the following snippet into your Framework project file:

<Project>
  ...
  <PropertyGroup>
    <BaseOutputPath>$(MSBuildProjectDirectory)/out/$(MSBuildProjectName)/bin</BaseOutputPath>
    <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/out/$(MSBuildProjectName)/obj</BaseIntermediateOutputPath>
  </PropertyGroup>
  ...
</Project>

It immediately worked for me, it was only later when I attentively read why we need it and why it works. I unexpectedly found it in part 2 of Migrating a Sample WPF App to .NET Core 3 under Making sure the .NET Framework project still builds section. BaseOutputPath and BaseIntermediateOutputPath msbuild variables can be found there, not sure if they are documented well anywhere.

Solution 3 - C#

That happened to me when opening a VS2015 project in VS2017. Deleting the project.assets.json from the obj folder did the trick.

Anyway, the Framework from the message was missing in the file, I did not add it there though, went with deleting it.

Solution 4 - C#

git clean -xdf

That should do the trick. It worked for us also in Jenkins. (we simply replayed the failed build with a modified script that ran git clean first).

For some reason MSBuild / Visual Studio get confused when switching between branches that target different versions of .NET Framework, so I had to do git cleans regularly while switching between branches.

Solution 5 - C#

The file that was causing issue for me was obj/project.assets.json inside project folder. Deleting it and rebuilding the project worked.

Solution 6 - C#

I had deleted the obj folder and rerun the build after choosing the target framework required in the property window it worked for me.

Solution 7 - C#

I up-voted Larissa but I thought it might be helpful to know how I got into this. I added a .net standard project file to my build (we target lots of platforms) and it produced the debris found in the obj folder. When the android sanity build came around, it threw up on the obj folder. My solution was to clean that folder out as a pre-build step. This is a difficult problem because it was working just fine for years...needle meet haystack.

Solution 8 - C#

For my case, delete the .pkgrefgen/ folder under the project folder works, it contains a file project.assets.json that refer to old .net framework

Solution 9 - C#

I ran into the same thing with .net 4.71. In my case, I simply migrated from packages.config to "package references" per

Migrate from packages.config to PackageReference

... and it fixed my issue. For me, I was going to do this anyway, so if you're going this way already, I'd just skip the above and migrate to package references.

Solution 10 - C#

Renaming the project solved the error for me. The issue happened after I created .NET Core project, then I deleted it and created a .NET Standard one with the same name. Obj folder was not present at all. Deleting bin folder, packages, clean and rebuild solution and getting latest with override did not help.

I have not tried this, but this thread proposed workaround is to include into csproj tag:

<ResolveNuGetPackages>false</ResolveNuGetPackages>

Solution 11 - C#

I am using a very old .NET project, and it was working fine until it stopped all of a sudden. Upgrading Visual Studio fixed for me thou.

Solution 12 - C#

On VS2019 I had to follow the error message and edit the project.json file that was in the project directory.

was ".NETFramework,Version=v4.0": {} // whatever the copied project was set to
now ".NETFramework,Version=v4.7.2": {} // set to whatever the current build is set to

Solution 13 - C#

Problem: In VS2017. Missing reference to .netframework 4.5.2, even though it was referenced as the target framework.

My solution: Verified framework was installed and restarted machine. After a git clean and simply right clicking on the solution in the explore and 'Restore nuget packages' did the trick.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionВасиль ТкачукView Question on Stackoverflow
Solution 1 - C#Larissa SavchekooView Answer on Stackoverflow
Solution 2 - C#moudrickView Answer on Stackoverflow
Solution 3 - C#tanukView Answer on Stackoverflow
Solution 4 - C#Shahin DohanView Answer on Stackoverflow
Solution 5 - C#AjithView Answer on Stackoverflow
Solution 6 - C#3LokView Answer on Stackoverflow
Solution 7 - C#Pale AleView Answer on Stackoverflow
Solution 8 - C#Yitong FengView Answer on Stackoverflow
Solution 9 - C#ebol2000View Answer on Stackoverflow
Solution 10 - C#K. B.View Answer on Stackoverflow
Solution 11 - C#Edgar FroesView Answer on Stackoverflow
Solution 12 - C#ergohackView Answer on Stackoverflow
Solution 13 - C#thefoyerView Answer on Stackoverflow