Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I "Update-Package" to a Previous Version in the Package Manager Console?

I have a version of a package installed in my project but during testing I have found a problem with it. I tried the obvious thing Update-Package -Id Foo.Bar -Version 1.0.0 -Force but the Update-Package cmdlet doesn't have a -Force parameter, and it doesn't allow updates to an earlier version. How do I downgrade my package dependencies (without taking advantage of source control!)


NOTE: This question is now irrelevant because Update-Package MyPackage -Version [an earlier version] works out of the box in recent versions of NuGet Package Manager. You don't even need a -Force switch.

like image 666
Damian Powell Avatar asked Feb 01 '12 16:02

Damian Powell


People also ask

How do I change NuGet package version?

Right-click the Packages folder in the project, and select Update. This will update the NuGet package to the latest version. You can double-click the Add packages and choose the specific version.

How do I reinstall a package in package manager console?

In Visual Studio, the Package Manager Console provides many flexible options for updating and reinstalling packages. On the Installed tab, select a package, record its name, then select Uninstall. Switch to the Browse tab, search for the package name, select it, then select Install).


2 Answers

I think I already have a solution to this so I place it here for (constructive) criticism.

function Reinstall-Package {      param(         [Parameter(Mandatory = $true)]         [string]         $Id,          [Parameter(Mandatory = $true)]         [string]         $Version,          [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]         [string]         $ProjectName,          [switch]         $Force     )      if (-not $ProjectName) {         $ProjectName = (get-project).ProjectName     }      Uninstall-Package -ProjectName $ProjectName -Id $Id -Force:$Force     Install-Package -ProjectName $ProjectName -Id $Id -Version $Version  } 

This allows us to use a call such as the following to update all references to a package within the current solution.

 Get-Project -All |      ?{ $_ | Get-Package | ?{ $_.Id -eq 'Foo.Bar' } } |          %{ $_ | Reinstall-Package -Id Foo.Bar -version 1.0.0 -Force } 

The -Force switch allows the package to be reinstalled even if it has dependent packages within the project.

like image 82
Damian Powell Avatar answered Sep 22 '22 21:09

Damian Powell


https://docs.nuget.org/consume/package-manager-console-powershell-reference

With NuGet 2.8 client or higher, Install-Package can be used to downgrade the existing packages in your project, if necessary. For example, if you had installed a pre-release version of a package to try out new features but would like to go back to a previous stable version you can do so using Install-Package (or Update-Package).

like image 26
Frol Avatar answered Sep 22 '22 21:09

Frol