liveBook · Manning

PackageReference

As explained in ASP.NET Core in Action, 3e

PackageReference is an MSBuild project-file item that declares a package dependency for a .NET project.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains XML declarations, command-line installation, restoration, and multiple-package setup without detailing dependency-resolution internals.
Examples
Uses Newtonsoft.Json and EF Core SQLite and Design package declarations as concrete project-file examples.
What it assumes
Assumes a .NET project and introduces NuGet packages, project-file editing, and package restoration.
What you can do after
Apply PackageReference through dotnet add package or direct XML to install and expose NuGet libraries.
How it connects
Links MSBuild project files, NuGet, .NET projects, and Entity Framework Core.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.