Nuget re-targeting after upgrading from .Net Framework 4.5 to 4.6.1

73,571

Solution 1

The packages will not be retargeted automatically, but there is an automated fix for this.

In Package Manager Console simply run:

Update-Package -Reinstall

This will force the package manager to reinstall every package in every project (without changing the version of the referenced package).

By reinstalling the packages after the new framework is targeted this changes all the references to the correct version.

You may also run this against a single project with :

Update-Package -Reinstall -ProjectName Project.Name.Here

I have used this technique many times to fix nuget reference issues.

Solution 2

1 Find all .csproj files and replace

<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>

with

<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>

2 Open Package Manager Console and run

Update-Package -Reinstall -IgnoreDependencies

3 Find all solutions then msbuild each one.

Solution 3

Fixed by using -

Update-Package -reinstall

enter image description here

Share:
73,571

Related videos on Youtube

Vishal
Author by

Vishal

Updated on July 08, 2022

Comments

  • Vishal
    Vishal almost 2 years

    I have a .net solution with approx 30 projects, all of them targeting .Net Framework 4.5. and each referencing at least 3-4 NuGet packages.

    We now need to update them to .Net Framework 4.6.1. So here's what I need to know:

    1. Do I need to re-target the NuGet packages as well or can I skip that since this will be an 'in-place' upgrade?
    2. If yes, can I just update the packages.config file for each project by replacing targetFramework="net45" with targetFramework="net461" for each NuGet package? I've seen a few threads recommending uninstall and then reinstall the package via 'update-package' command. I tried that today but it I ended up with a few errors.
    • Matt Ward
      Matt Ward about 8 years
      Modifying the targetFramework in the packages.config file has no affect on the assemblies that are referenced in the project, assuming you are not using an .xproj, so doing that would not be the correct change. I would guess that not many NuGet packages have assemblies that explicitly target .NET 4.6 so I suspect that nothing would need to be retargeted. You should be able to review the lib directories of your NuGet packages to see what they target.
    • Vishal
      Vishal about 8 years
      So I ended up upgrading 75% of my NuGet packages to their latest versions. Post the upgrade the targetFramework attribute was automatically set to 'net461' for most of the packages. There are some packages which I will be upgrading later and they seem to work fine post the upgrade. Thanks.
    • OzBob
      OzBob about 7 years
      upgrading from 4.5.* to 4.* will work be design: msdn.microsoft.com/en-us/library/ff602939%28v=vs.110%29.aspx
    • Ohad Schneider
      Ohad Schneider about 6 years
    • comecme
      comecme about 2 years
      Too bad nobody answered the first question. Is it needed to do this and if so, why?
  • Ken Hundley
    Ken Hundley over 7 years
    One comment to make this a little faster. Add -IgnoreDependencies: Update-Package -Reinstall -IgnoreDependencies
  • Ray
    Ray over 6 years
    Step # 3 should also automatically handle step # 2 for packages.config.
  • OzBob
    OzBob over 6 years
    @RayVega please explain a bit more. Could you edit and show how?
  • Ray
    Ray over 6 years
    If I skip step # 2 but instead do step # 3 immediately after step # 1, all of the packages.config files' targetFramework attributes automatically get modified to match the csproj's new TargetFramework. At least, that's how it worked for me using nuget.exe version 4.3.0.440 and upgrading from 4.5 -> 4.7. Essentially, it saved some work for me by not having to manually edit those packages files for each project.
  • Ohad Schneider
    Ohad Schneider about 6 years
    Theoretically speaking, couldn't a differently targeted package have different dependencies, in which case IgnoreDependencies might be unwise?
  • Ken Hundley
    Ken Hundley almost 6 years
    That is a good point. I have used IgnoreDependancies in the past, but mostly when reinstalling packages to fix reference issues, etc. I can definitely see where it could potentially cause problems for a Framework target update.
  • Can Bud
    Can Bud almost 6 years
    May I suggest the following for even more specific reinstalling: Update-Package Package.Name.Here -Reinstall -ProjectName Project.Name.Here
  • Paul Suart
    Paul Suart about 5 years
    This is great.... until you come to run it against a project that references pre-release packages. Any known workarounds?
  • Ken Hundley
    Ken Hundley about 5 years
    I believe you just need to add -IncludePrerelease flag
  • Glen Little
    Glen Little about 5 years
    Find and replace works well, but double-check web.config for the <compilation targetFramework="___"> element to ensure that you change it as well.
  • Tobias
    Tobias over 3 years
    The suggestion to skip step #2 from @Ray did not work for me. Not a single packages.config file was modified by just (re)building everything. I finally ended up with the full Update-Package -Reinstall which worked fine.
  • nrod
    nrod almost 2 years
    I was heading to .NET 4.8 and this did the trick. Thanks! 👏🏻