220

I have migrated a solution that is currently targeting .NET 4.0 in VS2010 to VS2012 and now I would like to re-target it to .Net 4.5

What I am not sure about is the NuGet packages. For example EF5, which I updated from EF4 in VS2010 turns out to be actually EF 4.4 as you can see here:

    <Reference Include="EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll</HintPath>
    </Reference>

I can also see the following in packages.config for the project:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="5.0.0" targetFramework="net40" />
</packages>

So my question is:

What is the best practice to re-target all NuGet packages that are currently set to target .NET 4.0 to target .NET 4.5?

Ivan Zlatev
  • 13,016
  • 9
  • 41
  • 50
  • https://stackoverflow.com/questions/6876732/how-do-i-get-nuget-to-install-update-all-the-packages-in-the-packages-config?rq=1 – Ohad Schneider Mar 28 '18 at 11:35

5 Answers5

278

NuGet 2.1 offers a feature that makes this a lot simpler: just do update-package -reinstall -ignoreDependencies from the Package Manager Console.

NuGet 2.0 doesn't handle re-targeting your applications very well. In order to change your packages' target frameworks, you must uninstall and reinstall the packages (taking note of the packages you had installed so that you can reinstall each of them).

The reason packages must be uninstalled and reinstalled is:

  • When installing a package, we determine the target framework of your project
  • We then match that up with the package contents, finding the appropriate \lib\ folder (and \content\ folder)
  • Assembly references are added with Hint Paths that point to the package's \lib\ folder, with the right subfolder (\lib\net40 for example)
  • Content files are copied from the packages \content\ folder, with the right subfolder (\content\net40 for example)
  • We record the targetFramework used to install the package within the packages.config file
  • After you change your project's target framework, the Hint Paths still point to net40
  • When you uninstall packages, we check the targetFramework that was recorded in packages.config to see what target framework's libs/content to remove from your project
  • When you reinstall the package, we detect your updated target framework and reference/copy the right libs/content
Tim Murphy
  • 4,892
  • 4
  • 40
  • 48
Jeff Handley
  • 4,134
  • 2
  • 23
  • 23
  • Using VS 2012 with an ASP.NET MVC 4 project and after re-targeting the .NET Framework from 4.0 to 4.5, I executed `update-package -reinstall` in Package Manager Console. All packages started to be uninstalled and updated and all of a sudden Windows 8 restarted and when it got back it told "Your PC run into a problem and restarted. Do you want to send information to Microsoft?" :( Scaring... By the way, this is the NuGet version I have installed right now: `2.2.40116.9051` Opened an issue here: http://nuget.codeplex.com/workitem/3049 – Leniel Maccaferri Feb 18 '13 at 15:16
  • 12
    the -reinstall options has never once worked for me. It either removes in the wrong order and errors on "can't remove X because Y depends on it" or sometimes just doesn't readd packages. Last time I tried it, it removed EntityFramework and then never re-added it. – CodingWithSpike Apr 16 '13 at 15:21
  • This is completely awful, by the way, if you want to compile a project against *both* CLR2 and CLR4. You can make a copy of your .csproj file, but your packages.config needs to stay packages.config ... – Jaykul Aug 02 '13 at 22:31
  • 4
    update-package -reinstall was not a solution for me. It also *updated* a lot of packages, rather than leaving them on the versions we use and had tested against. For instance, Ninject was moved up to v3, and that's a breaking version change. – Steve Owen Oct 08 '13 at 11:59
  • 13
    Do not even attempt the update-page -reinstall. This thing was such a mess when it ran on my local machine that I had to stop NuGet Package manager from going any further. It removed my jQuery 1.10 version and replaced it with 1.4.4 for some reason. Just do it manually and save yourself the hassle. – JustinMichaels Oct 08 '13 at 17:06
  • 2
    Agreed to the mess-thing, and that's even two years on from this post. It found lower versions of certain nugets, and screwed up a lot of references. And that was after nearly two hours of updating (on a high end workstation from early 2014). 20 projects in the solution. – Arve Systad Sep 23 '14 at 16:25
  • Also beware of changes in license agreements. – SepehrM Feb 13 '15 at 07:51
  • @CodingWithSpike I think you have missed the -ignoreDependencies flag. –  Jul 10 '16 at 23:54
45

For those who had problems with update-package -reinstall <packagename> command, consider running it with -ignoreDependencies flag, like this:

update-package -reinstall <packagename> -ignoreDependencies

This flag will leave your package dependencies alone, otherwise they might got updated even if the package you originally wanted reinstall still keeps it's version in same.

More info here.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
vpalmu
  • 524
  • 4
  • 13
  • Thanks, that really saves a lot of trouble. Watching Nuget trying to reinstall the 10 or so dependencies that EnterpriseLibrary tends to create, on 30+ projects, was heading towards a day long job. This brings it down to minutes. – David Keaveny Dec 10 '13 at 06:51
  • As others mentioned, very likely to break everything. – Gleno Jan 15 '15 at 09:25
  • 10
    You can automate this for the entire solution by changing it just slightly when running under the Package Manager Console: `get-package | % { update-package $_.Id -reinstall -ProjectName $_.ProjectName -ignoreDependencies }` – Kaleb Pederson May 06 '16 at 19:18
  • 2
    @KalebPederson In my experience the command works solution wide? –  Jul 10 '16 at 23:56
  • 1
    @BjörnAliGöransson - Sorry if I wasn't clear enough. The answer provides a way to update a single package across the solution. My script will go through every NuGet package in the solution and retarget it across the solution. The answer is perfect for a single project but the script I provided may be better if you have a lot of packages that need to be retargeted. – Kaleb Pederson Jul 11 '16 at 21:05
23

After trying the accepted answer unsuccessfully I would like to suggest a less risky command:

Update-Package <PackageName> -ProjectName <ProjectName> -Reinstall -IgnoreDependencies

For more info: http://blog.nuget.org/20121231/a-quick-tutorial-on-update-package-command.html

Bo Sunesen
  • 911
  • 1
  • 7
  • 9
  • 1
    According to the linked documentation `-reinstall` will only install the same version, so don't see any benefit to using `-safe`. Am I missing something? – Kaleb Pederson Jul 12 '16 at 21:14
4

Whilst attempting to reinstall packages solution wide, I encountered a dependency error (in spite of using the -ignoreDependencies flag), and all the packages.config files for every project had been deleted. In VS2013, it seems that packages.config does not get flushed back to disk and re-added until all the upgraded dependencies/references are re-attached to the project.

In my case what worked was to upgrade each project one-at-a-time by adding the -ProjectName projectname to the update-package command. In this case the packages.config is updated as each project is upgraded.

May not be practical for very large solutions but it seems a reasonable compromise to still take advantage of the automated upgrade for as many projects as possible and isolate the problematic ones without having every packages.config in your solution deleted on failure.

Craigology
  • 161
  • 3
  • 3
    I ran into the same issue. `UpdatePackage -Reinstall` deleted the package.config and project references for a few projects (specifically ones that had fakes assemblies generated in them). We worked around this by undoing all changes to the screwed up project and running: `Update-Package -reinstall -ProjectName "PROJECTNAME" -IgnoreDependencies` – MSC Sep 20 '16 at 20:51
2

With Visual Studio for Mac 2019, right-clicking the Packages folder shows 'Retarget' option in the menu. This resolved the retarget issue for all packages in the project that required retargeting. Looks like there was no NuGet Package Manager under Tools menu in Visual Studio for Mac (atleast in mine), so I couldn't launch Package Manager Console.

Retarget menu option under Packages right-click menu

Prabu Arumugam
  • 1,929
  • 2
  • 15
  • 19