Nuget re-targeting after upgrading from .Net Framework 4.5 to 4.6.1
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
Related videos on Youtube
Vishal
Updated on July 08, 2022Comments
-
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:
- Do I need to re-target the NuGet packages as well or can I skip that since this will be an 'in-place' upgrade?
- 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 about 8 yearsModifying 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 about 8 yearsSo 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 about 7 yearsupgrading from 4.5.* to 4.* will work be design: msdn.microsoft.com/en-us/library/ff602939%28v=vs.110%29.aspx
-
Ohad Schneider about 6 years
-
comecme about 2 yearsToo bad nobody answered the first question. Is it needed to do this and if so, why?
-
Ken Hundley over 7 yearsOne comment to make this a little faster. Add -IgnoreDependencies:
Update-Package -Reinstall -IgnoreDependencies
-
Ray over 6 yearsStep # 3 should also automatically handle step # 2 for packages.config.
-
OzBob over 6 years@RayVega please explain a bit more. Could you edit and show how?
-
Ray over 6 yearsIf 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 newTargetFramework
. 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 about 6 yearsTheoretically speaking, couldn't a differently targeted package have different dependencies, in which case
IgnoreDependencies
might be unwise? -
Ken Hundley almost 6 yearsThat 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 almost 6 yearsMay I suggest the following for even more specific reinstalling:
Update-Package Package.Name.Here -Reinstall -ProjectName Project.Name.Here
-
Paul Suart about 5 yearsThis is great.... until you come to run it against a project that references pre-release packages. Any known workarounds?
-
Ken Hundley about 5 yearsI believe you just need to add
-IncludePrerelease
flag -
Glen Little about 5 yearsFind and replace works well, but double-check web.config for the <compilation targetFramework="___"> element to ensure that you change it as well.
-
Tobias over 3 yearsThe 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 almost 2 yearsI was heading to .NET 4.8 and this did the trick. Thanks! 👏🏻