How to install . NET Core packages from Nuget using VS Code?

Asked

Viewed 6,139 times

7

I am using VS Code to develop a . NET Core project and would like to know how to install packages in Nuget on it.

In Visual Studio, there is a specific terminal for this, the Package Manager Console. In it you need to use the command Add-Package to install something.

For example, to install the Entity Framework you can use this command

PM> Add-Package EntityFramework

How can I install a package using Visual Studio Code?

2 answers

12


Using the command dotnet add of the . NET Core CLI

You can do this using the . NET Core CLI command dotnet add, the option package and the package name right after.

For example, to add the latest version of the Entity Framework.

$ dotnet add package EntityFramework

To specify a version, it is possible to use the argument --version.

$ dotnet add package EntityFramework --version=6.0

To specify the project where the dependency should be added you need to use the file path csproj of the project shortly after the add.

For example, to add the Entity Framework to the project ProjetoModels

$ dotnet add ProjetoModels.csproj package EntityFramework

This will check whether the version of the package is compatible with the project and, if compatible, will add the dependency on the project files and also download it using the command dotnet restore.

Tip: to open a terminal inside VS Code, in the project folder use the key combination Ctrl+'

Manually add dependency to XML

It is also possible to add the reference manually in the project file. For this just open the file the .csproj, search for the section ItemGroup and add an item PackageReference.

For example, to add the Entity Framework version 6.2.0.

<ItemGroup>
  <PackageReference Include="EntityFramework" Version="6.2.0" />
</ItemGroup>

After that, you need to run the command dotnet restore for the files to be downloaded.

3

Another option would be to use the extension

jmrog.vscode-nuget-package-manager

which allows you to add packages graphically, after installing the extension and restarting vscode, you need to:

  1. Open the "Command Palette" normally using Ctrl + shift + p, search for "Nuget Package Manager" and enter the name of the package you want to install, this is useful when you do not know the exact package name, as a list will be displayed with all the results that serve the search.
  2. Click on the desired package
  3. Choose the version
  4. Choose which project to install
  5. An option will be displayed suggesting to run the Restore in the project in which the package was added, or to perform the dotnet Restore manually

It is very useful when you are not sure about the full package name, or for people who prefer to work in a graphical way.

  • I liked the option, I didn’t know it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.