Nuget restore doesn't add reference to project - visual-studio-2013

Nuget keeps on not doing what I expect:
Say I got a bunch of references in a packages.config and add that to a newly created project. Then nuget asks me to restore the packages, on answering yes, it only downloads the packages to the packages folder but forgets to add a reference the the project.
How to automatically also add a reference to my project once restored?

Lasse is correct about the NuGet restore. NuGet restore will only download the NuGet packages and extract them to the packages directory.
However with just the package information in the packages.config file there is a way to have NuGet add the references to the project.
First restore the NuGet packages.
Next open the NuGet Package Manager Console and reinstall the packages using:
Update-Package -reinstall
The above -reinstall command will uninstall and install the packages again and fix any missing references.
There are some limitations with the above command which are documented on the NuGet site.

Restoring packages will only download the packages and place them into the right folders on disk.
You get the references when you originally add the nuget packages to the project the correct way.
You should not add nuget packages to the projects by modifying the package configuration file, you should add them using the "Add Nuget reference" menu item on the References folder of the project.

Related

NuGet reinstall updates the package to newer version

I have cleared all the local NuGet caches and Visual Studio 16.9 is unable to restore the packages.
It says:
All packages are already installed and there is nothing to restore.
When I build the project, I get many errors as the NuGet packages are not restored.
The project has to contain the exact same packages as they are in the PackageReferences inside the csproj file and I've read that using the Package Manager Console can help by executing the Update-Package -reinstall command.
The Install-Package command does not provide an option to force a reinstall, so use Update-Package -reinstall instead.
I tried it and now I have this in my log:
No package updates are available from the current package source for project....
Updates? I don't want to update anything...
Restored D:\Dev\MyProject01\MyProject01.csproj (in 29 ms).
Successfully uninstalled 'Acr.UserDialogs 7.1.0.475' from MyProject01
Successfully installed 'Acr.UserDialogs 7.1.0.481' to MyProject01
How is this a reinstall? Seems more like updating the package to a newer version...
How could I reinstall the same packages?
UPDATE:
When I use the Rebuild command in the Solution Explorer, this is what I see.
Actually, that right-click on the Solution-->Restore nuget packages is for the missing project packages folder with packages.config. And it cannot detect the global cache missing packets alone.
That is why Microsoft has enabled these two settings:
So you do not have to worry about restore step. Just click Rebuild button rather than Build button. The two restore settings are under Rebuild process.
The project has to contain the exact same packages as they are in the
PackageReferences inside the csproj file
update-package -reinstall command is for non-sdk style projects with packages.config nuget management format and always reinstall the same version. And I wonder if your solution has the non-sdk project that used packages.config and that project can use the command.
I have a non-sdk project with net framework 4.5.2. See:
But update-package -reinstall does not work on new-sdk projects.
ConsoleApp1 is a net core project.
So, I wonder if you use Update-Package under it. And that command is to update the old version to the new one which can works on both non-sdk projects and new-sdk projects.
Conclusion
When you face the problem next time, just click Rebuild button. It is more easier.
Try deleting your project's obj/ directory (this is where NuGet stores information about which packages it's decided to use), and then rerunning the restore.
If you have source dependencies (i.e. ProjectReference) you may need to delete their obj/ directories too

Install multiple NuGet packages at once

Is it possible to use nuget to install multiple packages at once? That is, both download all nuget packages listed in packages.config and add the packages to the .csproj file.
What I do now is to use the NuGet Package Manager in Visual Studio and install each package one by one, but is there an easier way?
EDIT:
This question is not solved by How do I get NuGet to install/update all the packages in the packages.config? as it only downloads and installes the packages to the packages directory, and does not change the project files.
If I run
nuget install packages.config
It will install all packages in the packages.config file, but not update the project file.
Could you try to run this from the package manager console in Visual Studio:
Update-Package –reinstall
That should fix missing assembly references in your *.csproj files, if the packages are already in your packages.config.

Restore nuget packages defined in .nuget folder

There are some nuget packages (e.g. OpenCover or ReportGenerator) installed without changing packages.config in any of the project, but there is a [Solution Dir]\.nuget\packages.config created with the package reference information.
When VS builds the solution, the packages defined in that file are not downloaded at all (even I have auto restore nuget enabled).
How can I restore them automatically?
The MSBuild based package restore, that uses NuGet.targets, which is enabled in Visual Studio by selecting Enable NuGet Package Restore, does not seem to support restoring solution level packages, which are those that are defined in the [SolutionDir]\.nuget\packages.config file.
Looking at the NuGet.targets file on build it restores the packages for the project using the project's packages.config file but does not use the solution's packages.config file.
So your options are:
Stop using the MSBuild based package restore. Visual Studio, with recent versions of NuGet, will automatically restore all packages, including all solution level packages, when you build.
Run NuGet.exe restore YourSolution.sln from the command line, or PowerShell console, or as a pre-build step, to restore all packages. NuGet.exe can be used to restore all packages on a build server if you are using one.
The MSBuild based package restore has been deprecated by the NuGet team so I would use option 1) if this is possible.

NuGet restore enabled but I still get an error

Why do I get the following build output error when I already have NuGet package restore enabled?
Restoring NuGet packages...
To prevent NuGet from downloading packages during build, open the
Visual Studio Options dialog, click on the Package Manager node and
uncheck 'Allow NuGet to download missing packages'.
All packages listed in packages.config are already installed.
MyProject.csproj: error : This project references NuGet package(s)
that are missing on this computer. Enable NuGet Package Restore to
download them.
It only happens on one project.
I am using Visual Studio 2013 and NuGet 2.8.
Make sure to upgrade to the latest version of NuGet which does package restore automatically. See this post by David Ebbo for more information: http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html
You're going to want to delete the NuGet targets (delete the .nuget folder and then manually edit your .csproj files and remove the lines that import the NuGet.targets file). This is no longer needed. When you compile your solution, NuGet will restore it.

Reinstalling NuGet packages with NuGet installed as VS Extension

I would like to be able to install all of the NuGet packages in the packages.config, as per The NuGet docs. NuGet is installed as a VS Extension, and I can't seem to find nuget.exe. Is it possible to run:
nuget i packages.config -o Packages
Without maintaining a seperate copy of nuget.exe on a per project basis?
Reinstall all packages in all projects of the current solution:
Update-Package -Reinstall
You can find more information about reinstalling nuget packages here
Warning - using
Update-Package -Reinstall
or
Update-Package -Reinstall -IgnoreDependencies
may remove all of your packages and package.config files!
Always make sure that you have your backups performed first.
Scenario:
Solution with multiple projects
Each contains their own Nuget entries, some with the same packages (e.g., SharpRepository, Entity Framework)
Now copy folder without the packages folder for "distribution" somewhere else
Assume the packages folder wasn't included with the distribution
Now try the command Update-Package -Reinstall or if you have some alpha packages and/or are sure your dependencies are good, try Update-Package -Reinstall -IgnoreDependencies
Result:
Because the packages folder doesn't exist, the entries for your packages methodically go away, too. This can surprise some people - so be careful, is all I'm saying.
As an update to this post, NuGet 1.6 added support for the workflow to restore the packages at build time if missing.
Right-Click on the solution, click "Enable Package Restore Mode" to set it on.
More details at:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
Found the solution on This blog entry. I needed to install NuGet.CommandLine, which makes nuget.exe. globally available in the VS command line. I can then set this up as a pre-build event to ensure that dependencies are downloaded.
A much easier option that you can keep enabled in Visual Studio during development to ease off your NuGet package installation related pain.
Keep both below mentioned options under NuGet Package Manager > General in checked state -
Allow NuGet to download missing packages
Automatically check for missing packages during build in Visual Studio
Have a look at the screenshot below:

Resources