193

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.
Cœur
  • 37,241
  • 25
  • 195
  • 267
Vishal
  • 2,103
  • 2
  • 16
  • 18
  • 1
    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. – Matt Ward Mar 15 '16 at 23:31
  • 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. – Vishal Apr 07 '16 at 15:39
  • upgrading from 4.5.* to 4.* will work be design: https://msdn.microsoft.com/en-us/library/ff602939%28v=vs.110%29.aspx – OzBob Feb 17 '17 at 08:29
  • https://learn.microsoft.com/en-us/nuget/consume-packages/reinstalling-and-updating-packages – Ohad Schneider Mar 28 '18 at 11:32
  • 2
    Too bad nobody answered the first question. Is it needed to do this and if so, why? – comecme Feb 23 '22 at 07:44

3 Answers3

367

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.

Ken Hundley
  • 3,833
  • 1
  • 13
  • 8
  • 16
    One comment to make this a little faster. Add -IgnoreDependencies: `Update-Package -Reinstall -IgnoreDependencies` – Ken Hundley Dec 14 '16 at 17:18
  • 12
    Theoretically speaking, couldn't a differently targeted package have different dependencies, in which case `IgnoreDependencies` might be unwise? – Ohad Schneider Mar 28 '18 at 11:30
  • 1
    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. – Ken Hundley May 13 '18 at 04:01
  • 2
    May I suggest the following for even more specific reinstalling: `Update-Package Package.Name.Here -Reinstall -ProjectName Project.Name.Here` – Can Bud Jul 05 '18 at 12:00
  • 1
    This is great.... until you come to run it against a project that references pre-release packages. Any known workarounds? – Paul Suart Mar 13 '19 at 15:18
  • 3
    I believe you just need to add `-IncludePrerelease` flag – Ken Hundley Mar 14 '19 at 17:09
  • I was heading to .NET 4.8 and this did the trick. Thanks! – nrod Jun 01 '22 at 16:47
52

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.

Evren Kuzucuoglu
  • 3,781
  • 28
  • 51
OzBob
  • 4,227
  • 1
  • 39
  • 48
  • Step # 3 should also automatically handle step # 2 for _packages.config_. – Ray Jan 29 '18 at 22:19
  • @RayVega please explain a bit more. Could you edit and show how? – OzBob Jan 29 '18 at 22:23
  • 3
    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. – Ray Jan 29 '18 at 22:33
  • 1
    Find and replace works well, but double-check web.config for the element to ensure that you change it as well. – Glen Little Mar 18 '19 at 21:20
  • 2
    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. – Tobias Aug 12 '20 at 11:11
5

Fixed by using -

Update-Package -reinstall

enter image description here

Deep
  • 1,025
  • 11
  • 7