Before we dive into the details of NuGet, we first need to understand what NuGet is and what it is used for. Nuget is a library sharing mechanism. It allows developers to create and share packages. These packages are containers for code. Microsoft uses NuGet to publish a number of useful packages (Entity Framework is one very useful example.) Nuget can be used by any developer to share code with the Visual Studio Community.
Nuget has two primary interfaces:
- The Package Manager User Interface (UI). The UI provides a quick, simple, and full featured interface for adding packages to your project. You can use it to add, update, remove, and enumerate packages in your solution.
- The Package Manager Console. The Console is basically a Powershell interface into the Nuget Library. It allows you to work with the library using PowerShell Command. For example, Get-Package-ListAvailable will list all the available packages in the default source location.
We will examine each interface in turn and describe how to use it within the context of a Visual Studio project.
Let's begin by exploring the user interface (UI):
- Open the UI, right-click on project-references, and choose manage NuGet Packages.
OR from the menu bar select Tools-NuGet Package Manager-Manage Nuget Packages
- The Browse tab is selected by default. From the Browse tab, you can search for the package you want to install. For example, in the search box, type "Entity" to search for the Entity Framework Package.
- Select the appropriate version (see note below) and which projects (if any) in the solution into which you would like to install the package. Then click Install.
- NuGet will download the package, install it on the local maching and add a reference to it in the selected project.
- Once a package is installed, you can use the NuGet manager to upgrade the package (if a new version or updates are released). You can also use it to uninstall packages from your solution.
Now let's take a look at the Package Manager Console.
- To open the console, select Tools-Nuget Package Manager-Package Manager Console
- Select package source and default project as appropriate
- Enter Powershell commands as appropriate. For example:
- Install-Package EntityFramework
- Update-Package EntityFramework
Note: When working with packages, be aware of package version numbers. Often, package updates can contain breaking changes that require extensive changes to your project.