How to restore NuGet packages from the console when Visual Studio refuses

How to restore NuGet packages from the console when Visual Studio refuses

On occasion, and especially if I switch branches in git, my Visual Studio projects will load without any of the dependencies they rely on (generally in the packages folder*). Telling Visual Studio to restore all packages in various ways (including a full re-build) always seems to fail... Visual Studio's package manager thinks everything is already up to date... which it clearly is not.

So, command line to the rescue...

First, make sure you have the Package Manager Console open (Tools > NuGet Package Manager > Package Manager Console) and enter the following command:

Update-Package -reinstall
Attempting to gather dependency information for multiple packages with respect to project 'openid', targeting '.NETFramework,Version=v4.7.2'
Gathering dependency information took 1.57 min
Attempting to resolve dependencies for multiple packages.
Resolving dependency information took 0 ms
Resolving actions install multiple packages
etc...

If you need to only update packages for a specific project (rather than the whole solution) just use the following:

Update-Package -reinstall -project [ProjectName]

If you're using the newer Package Reference method of including packages in your project (as opposed to the packages.config file method) you probably won't suffer this problem as the solution keeps references to the packages internally.

See also: https://docs.microsoft.com/en-us/nuget/consume-packages/reinstalling-and-updating-packages