Nuget packages are great. Concept is to “plug-in” whole libraries or frameworks with correct configurations and files at right place. So that it is all central and ready to use. As you install package, it add required assemblies to project, and add all the related configuration to config file automatically using one command. Sometime it can get messy in terms of package version management. if your code is not compatible with new version of a package, or you want to un-install or simply revert to an old version of package. I will go through how to install, un-install a package, and how to install specified version. Easiest way to install package is through Nuget Package Manager GUI, but if you want to have a bit more control, best way to manage packages in projects or solution is through Package Manager Console.
To access Nuget Package Manager Console:
Package Manager Console looks like following. It has two major options along with Console:
Package Source: By Default it is set to nuget.org, You can add / remove package sources in your list.
Default Project: Select the project on which you want to manage Nuget packages. It displays list of projects from opened Solution.
To get or search list of available packages in Package Source Library (e.g. Nuget.org)
Package Sources like Nuget.org contains library of packages. To see the List of all Available Packages on package source
get-package –ListAvailable
will retrieve the list of all available packages.
Normally there are so many packages, that going through the whole list is of no benefit. So you can use filter the list by –filter parameter. Such as to search all packages which have word ‘Mapper’ in name. Use:
get-package -ListAvailable -filter Mapper
will retrieve the list of only packages with name having ‘Mapper’ in it.
If you remove –ListAvailable parameter, get-package will show the list of packages already installed and available locally.
Install an available package
Once you know the name of package. Command for installing package is:
install-package <packageName>
for example, if we want to install latest version of EntityFramework, we would use
install-package EntityFramework
will install latest version of EntityFramework compatible with your Visual Studio and .NET framework
Uninstalling an installed package
You have realized that you have installed wrong version of package, best way is to first remove installed package. For any other reason, if you want to remove a package, use following command:
uninstall-package <packageName>
We want to uninstall above installed EntityFramework 6.0.1. it would be
uninstall-package EntityFramework
This would uninstall package. Some time it require a Visual Studio restart, if package manage is unable to delete any existing file in use.
Installing a Specified Version of package
You can mention version number along with package name to install any old package. For example, in above example, we have uninstalled EntityFramework version 6.0.1, and we want to install version 5.0.0. Command would be:
install-package EntityFramework -version 5.0.0
This should install Version 5.0.0 of EntityFramework. In above case, since Version 5 was already installed on another project in solution, so it just enabled version 5 for TestConsoleApp.
Summary:
Nuget packages are great for productivity and to re-use already developed libraries. For package management, best tool is to use Package Manager Console which gives you more control over details such as versions.
For exploring more about Nuget Packages, please refer to official documentation.