LinuxCommandLibrary

dotnet-add-reference

Add project-to-project references to a project

TLDR

Add a reference to the project in the current directory

$ dotnet add reference [path/to/reference.csproj]
copy

Add multiple references to the project in the current directory
$ dotnet add reference [path/to/reference1.csproj path/to/reference2.csproj ...]
copy

Add a reference to the specific project
$ dotnet add [path/to/project.csproj] reference [path/to/reference.csproj]
copy

Add multiple references to the specific project
$ dotnet add [path/to/project.csproj] reference [path/to/reference1.csproj path/to/reference2.csproj ...]
copy

SYNOPSIS

dotnet add [<PROJECT>] reference [-f|--framework <FRAMEWORK>] [--interactive] [<PROJECT_REFERENCES> ...]

PARAMETERS

<PROJECT>
    Path to the project file to modify. Defaults to current directory if omitted.

<PROJECT_REFERENCES> ...
    One or more paths to projects to reference (relative or absolute).

-f|--framework <FRAMEWORK>
    Adds reference only for the specified target framework (e.g., net6.0).

--interactive
    Enables interactive mode, pausing for user input if needed.

-v|--verbosity <LEVEL>
    Sets output verbosity (quiet|minimal|normal|detailed|diagnostic).

DESCRIPTION

The dotnet add reference command is a .NET CLI tool used to add one or more project-to-project references to a specified .NET project file, typically a .csproj file. It automatically modifies the project file by inserting <ProjectReference Include="..." /> elements, enabling dependencies between projects in a solution without needing a .sln file upfront.

This command streamlines multi-project development workflows, ensuring referenced projects are built in the correct order during dotnet build or dotnet restore. Paths to referenced projects can be relative (recommended for portability) or absolute. It supports targeting specific frameworks via the --framework option, useful for multi-targeting projects.

In interactive mode (--interactive), it pauses for user input if issues like authentication arise, though rare for project references. After execution, run dotnet restore to update dependencies. This promotes modular .NET application architecture, especially in microservices or libraries.

CAVEATS

Referenced projects must share compatible target frameworks; relative paths preferred for source control. Always run dotnet restore afterward. Fails if project paths invalid or duplicates exist.

EXAMPLES

dotnet add reference ../lib/MyLibrary.csproj
Adds relative reference.

dotnet add --framework net6.0 reference ../webapp/WebApp.csproj
Framework-specific reference.

HISTORY

Introduced in .NET Core 2.0 SDK (2017) to support SDK-style project files and simplify reference management over MSBuild edits.

SEE ALSO

dotnet remove reference(1), dotnet list reference(1), dotnet restore(1), dotnet build(1)

Copied to clipboard