'Compile error after adding .net6 nuget packages to .net6 project

I have Visual Studio 2022 version 17.1.6, .net Sdk version 6.0.202, moreover I have several other .net versions installed including core 3.1.0, 3.1.22, 3.1.24, I reparired the installation of all of them, installing both runtime and SDK when available

I have this problem: I create a new c# console application targeting .net6 using the "new project" wizard, and it runs.

I then add any Microsoft NuGet package in version 6.x (let's say Microsoft.EntityFrameworkCore) and I see in Error list:

error MSB4057: The target "NETStandardCompatError_System_Runtime_CompilerServices_Unsafe_netcoreapp3_1" does not exist in the project.

The same error appears in Output if I build the solution.

I zipped the project with references and sent it to a collegue, it compiles on his pc.

I tried to add a reference to EntityFrameworkCore version 5 and it works perfectly. I have the same issue also with other nuget packages: version 6 triggers the MSB4057 error, version 5 works.

The System.Runtime.CompilerServices.Unsafe does not appear as direct reference in my project, it is actually referenced indirectly by other assemblies, such as Microsoft.Externsions.Caching.Memory, Microsoft.Extensions.Options and others.

In Solution explorer I see correctly the references, there are no "danger" icons and the path C:\Users\lucav.nuget\packages\system.runtime.compilerservices.unsafe\6.0.0 actually contains the dll

In Nuget I cannot find system.runtime.compilerservices.unsafe 3.1

I checked with Winmerge the differences between the vanilla hello world without references and the one with a reference added, those are the results:

.csProj

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
</ItemGroup>

project.assets.json replicates the references structure seen in solution explorer, it contains references to "System.Runtime.CompilerServices.Unsafe": "6.0.0"

such as

      "System.Runtime.CompilerServices.Unsafe/6.0.0": {
        "type": "package",
        "compile": {
          "lib/net6.0/System.Runtime.CompilerServices.Unsafe.dll": {}
        },
        "runtime": {
          "lib/net6.0/System.Runtime.CompilerServices.Unsafe.dll": {}
        },
        "build": {
 

     "buildTransitive/netcoreapp2.0/System.Runtime.CompilerServices.Unsafe.targets": {}
    }
  

and, above all, this one that has a reference to netcoreapp3.1

"System.Runtime.CompilerServices.Unsafe/6.0.0": {
      "sha512": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==",
      "type": "package",
      "path": "system.runtime.compilerservices.unsafe/6.0.0",
      "files": [
        ".nupkg.metadata",
        ".signature.p7s",
        "Icon.png",
        "LICENSE.TXT",
        "THIRD-PARTY-NOTICES.TXT",
        "buildTransitive/--netcoreapp3.1/_._",
        "buildTransitive/netcoreapp2.0/System.Runtime.CompilerServices.Unsafe.targets",
        "lib/net461/System.Runtime.CompilerServices.Unsafe.dll",
        "lib/net461/System.Runtime.CompilerServices.Unsafe.xml",
        "lib/net6.0/System.Runtime.CompilerServices.Unsafe.dll",
        "lib/net6.0/System.Runtime.CompilerServices.Unsafe.xml",
        **"lib/netcoreapp3.1/System.Runtime.CompilerServices.Unsafe.dll",**
        "lib/netcoreapp3.1/System.Runtime.CompilerServices.Unsafe.xml",
        "lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.dll",
        "lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.xml",
        "system.runtime.compilerservices.unsafe.6.0.0.nupkg.sha512",
        "system.runtime.compilerservices.unsafe.nuspec",
        "useSharedDesignerContext.txt"
      ]
    }

and finally the file *.csproj.nuget.g.targets

the version without nuget packages is:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
  </PropertyGroup>
</Project>

after adding the reference to EntityFrameworkCore it becomes

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
    <Import Project="$(NuGetPackageRoot)system.runtime.compilerservices.unsafe\6.0.0\buildTransitive\netcoreapp2.0\System.Runtime.CompilerServices.Unsafe.targets" Condition="Exists('$(NuGetPackageRoot)system.runtime.compilerservices.unsafe\6.0.0\buildTransitive\netcoreapp2.0\System.Runtime.CompilerServices.Unsafe.targets')" />
  </ImportGroup>
</Project>

There are no more relevant differences in files.

What should I check/install in order to fix this compile error?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source