PackageReference is an MSBuild project-file item that adds a NuGet package dependency to a .NET project. The package becomes available to the project’s source code after restoration. section 3.5
Use PackageReference to add reusable libraries and configure their package versions:
- Run dotnet add package Newtonsoft.Json from the project folder to update the .csproj file and restore the package. section 3.5
- Declare a package with Include and Version attributes inside an ItemGroup. section 3.5
- Install several related packages when a framework is modular, as with EF Core’s SQLite provider and design package. section 12.2.1
Adding Newtonsoft.Json
Add <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> inside the project file’s ItemGroup. After restoration, the Newtonsoft.Json namespace and classes are available in source files. section 3.5
Listing 12.1 Installing EF Core in an ASP.NET Core application
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework> #1
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference #2
Include="Microsoft.EntityFrameworkCore.SQLite" #2
Version="7.0.0" /> #2
<PackageReference #3
Include="Microsoft.EntityFrameworkCore.Design" #3
Version="7.0.0" > #3
<IncludeAssets>runtime; build; native; contentfiles; #4
Analyzers; buildtransitive</IncludeAssets> #4
<PrivateAssets>all</PrivateAssets> #4
</PackageReference>
</ItemGroup>
Use the EF Core project-file listing to see multiple PackageReference elements, versions, and private build-time assets together. section 12.2.1
