Question:
I'm using VS Code to develop a .NET Core project and I would like to know how to install Nuget packages on it.
In Visual Studio, there is a specific terminal for this, the Package Manager Console . In it you have to use the Add-Package
command to install something.
For example, to install Entity Framework one can use this command
PM> Add-Package EntityFramework
How can I install a package using Visual Studio Code?
Answer:
Using the .NET Core CLI dotnet add
command
You can do this using the .NET Core CLI with the dotnet add
command, the package
option, and the package name right after it.
For example, to add the latest version of Entity Framework.
$ dotnet add package EntityFramework
To specify a version, you can use the --version
argument.
$ dotnet add package EntityFramework --version=6.0
To specify the project where the dependency should be added, you need to use the csproj file path of the project right after the add
.
For example to add the Entity Framework in the ProjetoModels
project
$ dotnet add ProjetoModels.csproj package EntityFramework
This will verify that the package version is compatible with the project and, if compatible, will add the dependency in the project files and also download it using the dotnet restore
command.
Tip : to open a terminal inside VS Code, in the project folder use the key combination Ctrl + '
Add dependency manually in XML
It is also possible to manually add the reference in the project file. To do this, just open the .csproj file, look for the ItemGroup
section and add a PackageReference
item.
For example, to add Entity Framework version 6.2.0 .
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.2.0" />
</ItemGroup>
After that, you need to run the dotnet restore
command so that the files are downloaded.