Skip to content

.NET Core Commands

Essential .NET Core CLI commands for development, building, publishing, and package management.

Publishing Applications

Cross-Platform Publishing

bash
# Publish for Linux (64-bit)
dotnet publish --configuration release --runtime linux-x64

# Publish for macOS (64-bit)
dotnet publish --configuration release --runtime osx-x64 --self-contained true -p:PublishSingleFile=true

# Publish for Windows (64-bit)
dotnet publish --configuration Release --runtime win-x64 --self-contained -p:PublishSingleFile=true -p:PublishTrimmed=true

Runtime Identifiers (RIDs)

Common runtime identifiers for publishing:

OSArchitectureRID
Windowsx64win-x64
Windowsx86win-x86
WindowsARM64win-arm64
Linuxx64linux-x64
LinuxARMlinux-arm
LinuxARM64linux-arm64
macOSx64osx-x64
macOSARM64 (M1)osx-arm64

Publishing Options

bash
# Self-contained deployment (includes .NET runtime)
dotnet publish --runtime win-x64 --self-contained true

# Framework-dependent deployment (requires .NET runtime on target)
dotnet publish --runtime win-x64 --self-contained false

# Single file executable
dotnet publish --runtime win-x64 -p:PublishSingleFile=true

# Trim unused assemblies (reduce size)
dotnet publish --runtime win-x64 -p:PublishTrimmed=true

# Ready to run (improve startup time)
dotnet publish --runtime win-x64 -p:PublishReadyToRun=true

# Output directory
dotnet publish --output ./publish/

Build Configurations

bash
# Debug build (default)
dotnet publish --configuration Debug

# Release build (optimized)
dotnet publish --configuration Release

# Custom configuration
dotnet publish --configuration Production

Complete Publishing Examples

Windows Executable:

bash
dotnet publish \
  --configuration Release \
  --runtime win-x64 \
  --self-contained true \
  -p:PublishSingleFile=true \
  -p:PublishTrimmed=true \
  -p:PublishReadyToRun=true \
  --output ./publish/win-x64

Linux Server:

bash
dotnet publish \
  --configuration Release \
  --runtime linux-x64 \
  --self-contained false \
  --output /var/www/myapp

macOS Application:

bash
dotnet publish \
  --configuration Release \
  --runtime osx-x64 \
  --self-contained true \
  -p:PublishSingleFile=true \
  --output ./publish/osx-x64

Project Management

Create Projects

bash
# Create console application
dotnet new console -n MyApp

# Create web API
dotnet new webapi -n MyApi

# Create ASP.NET Core web app
dotnet new webapp -n MyWebApp

# Create class library
dotnet new classlib -n MyLibrary

# Create xUnit test project
dotnet new xunit -n MyApp.Tests

# Create solution
dotnet new sln -n MySolution

Solution Management

bash
# Add project to solution
dotnet sln add ./MyApp/MyApp.csproj

# Add multiple projects
dotnet sln add **/*.csproj

# Remove project from solution
dotnet sln remove ./MyApp/MyApp.csproj

# List projects in solution
dotnet sln list

Project References

bash
# Add project reference
dotnet add reference ../MyLibrary/MyLibrary.csproj

# Remove project reference
dotnet remove reference ../MyLibrary/MyLibrary.csproj

# List project references
dotnet list reference

Building

Build Commands

bash
# Build project
dotnet build

# Build with specific configuration
dotnet build --configuration Release

# Build without restoring packages
dotnet build --no-restore

# Build and show detailed output
dotnet build --verbosity detailed

# Clean build output
dotnet clean

# Clean and rebuild
dotnet clean && dotnet build

Running Applications

Run Commands

bash
# Run application
dotnet run

# Run with arguments
dotnet run -- --arg1 value1 --arg2 value2

# Run specific project
dotnet run --project ./MyApp/MyApp.csproj

# Run with specific configuration
dotnet run --configuration Release

# Watch for changes and restart (development)
dotnet watch run

Watch Mode

bash
# Watch and rebuild on changes
dotnet watch build

# Watch and run tests
dotnet watch test

# Watch specific project
dotnet watch --project ./MyApp/MyApp.csproj run

NuGet Package Management

Clear NuGet Cache

bash
# Clear all NuGet caches (from README)
dotnet nuget locals all --clear

# Clear specific caches
dotnet nuget locals http-cache --clear
dotnet nuget locals global-packages --clear
dotnet nuget locals temp --clear

Package Operations

bash
# Add NuGet package
dotnet add package Newtonsoft.Json

# Add specific version
dotnet add package Newtonsoft.Json --version 13.0.1

# Remove package
dotnet remove package Newtonsoft.Json

# List packages
dotnet list package

# List outdated packages
dotnet list package --outdated

# Update packages
dotnet add package Newtonsoft.Json --version 13.0.2

Package Sources

bash
# List NuGet sources
dotnet nuget list source

# Add NuGet source
dotnet nuget add source https://myget.org/F/myfeed/api/v3/index.json --name MyFeed

# Remove NuGet source
dotnet nuget remove source MyFeed

# Enable source
dotnet nuget enable source MyFeed

# Disable source
dotnet nuget disable source MyFeed

Testing

Run Tests

bash
# Run all tests
dotnet test

# Run tests with detailed output
dotnet test --logger "console;verbosity=detailed"

# Run tests and collect coverage
dotnet test --collect:"XPlat Code Coverage"

# Run specific test
dotnet test --filter FullyQualifiedName=MyNamespace.MyClass.MyTest

# Run tests matching pattern
dotnet test --filter "Name~Integration"

# Run tests without building
dotnet test --no-build

Test Filters

bash
# Run by category
dotnet test --filter Category=Unit

# Run by trait
dotnet test --filter Priority=1

# Run by class name
dotnet test --filter ClassName=MyTests

# Combine filters
dotnet test --filter "Category=Unit&Priority=1"

Database Migrations (Entity Framework Core)

Migration Commands

bash
# Install EF Core tools globally
dotnet tool install --global dotnet-ef

# Update EF Core tools
dotnet tool update --global dotnet-ef

# Add migration
dotnet ef migrations add InitialCreate

# Update database
dotnet ef database update

# Remove last migration
dotnet ef migrations remove

# List migrations
dotnet ef migrations list

# Generate SQL script
dotnet ef migrations script

# Drop database
dotnet ef database drop

Common EF Core Workflows

bash
# Create initial migration and database
dotnet ef migrations add InitialCreate
dotnet ef database update

# Add new migration after model changes
dotnet ef migrations add AddUserTable
dotnet ef database update

# Rollback to specific migration
dotnet ef database update PreviousMigration

# Generate SQL script for production
dotnet ef migrations script --output migration.sql

Tools Management

Global Tools

bash
# List installed global tools
dotnet tool list --global

# Install global tool
dotnet tool install --global dotnet-ef

# Update global tool
dotnet tool update --global dotnet-ef

# Uninstall global tool
dotnet tool uninstall --global dotnet-ef

# Search for tools
dotnet tool search entityframework

Local Tools

bash
# Create tool manifest
dotnet new tool-manifest

# Install local tool
dotnet tool install dotnet-ef

# Run local tool
dotnet tool run dotnet-ef

# Update local tool
dotnet tool update dotnet-ef

# List local tools
dotnet tool list

Information Commands

Version Information

bash
# Display .NET SDK version
dotnet --version

# Display detailed info
dotnet --info

# List installed SDKs
dotnet --list-sdks

# List installed runtimes
dotnet --list-runtimes

Project Information

bash
# Display project file
cat MyApp.csproj

# Display project properties
dotnet msbuild MyApp.csproj /t:Properties

# Check target framework
dotnet msbuild MyApp.csproj /t:GetTargetFramework

Docker Integration

Dockerfile for .NET

dockerfile
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

# Publish stage
FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish

# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]

Docker Commands

bash
# Build Docker image
docker build -t myapp:latest .

# Run container
docker run -d -p 8080:80 --name myapp myapp:latest

# Multi-stage build with cache
docker build --target publish -t myapp:latest .

Performance and Diagnostics

Diagnostics Tools

bash
# Install diagnostics tools
dotnet tool install --global dotnet-counters
dotnet tool install --global dotnet-trace
dotnet tool install --global dotnet-dump

# Monitor process counters
dotnet-counters monitor --process-id <PID>

# Collect trace
dotnet-trace collect --process-id <PID>

# Capture memory dump
dotnet-dump collect --process-id <PID>

Common Workflows

New Web API Project

bash
# Create project
dotnet new webapi -n MyApi
cd MyApi

# Add packages
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Swashbuckle.AspNetCore

# Run in development
dotnet run

# Publish for production
dotnet publish --configuration Release --runtime linux-x64 --output ./publish

New Console Application

bash
# Create project
dotnet new console -n MyConsole
cd MyConsole

# Add packages
dotnet add package Newtonsoft.Json

# Run
dotnet run

# Publish single executable
dotnet publish --configuration Release --runtime win-x64 --self-contained -p:PublishSingleFile=true

Library with Tests

bash
# Create solution
dotnet new sln -n MyLibrary

# Create library project
dotnet new classlib -n MyLibrary.Core
dotnet sln add MyLibrary.Core/MyLibrary.Core.csproj

# Create test project
dotnet new xunit -n MyLibrary.Tests
dotnet sln add MyLibrary.Tests/MyLibrary.Tests.csproj

# Add project reference
cd MyLibrary.Tests
dotnet add reference ../MyLibrary.Core/MyLibrary.Core.csproj

# Run tests
dotnet test

Troubleshooting

Common Issues

Clear NuGet cache:

bash
dotnet nuget locals all --clear
dotnet restore

Build errors after package update:

bash
dotnet clean
dotnet restore
dotnet build

Runtime not found:

bash
# Check installed runtimes
dotnet --list-runtimes

# Install required runtime or publish as self-contained
dotnet publish --self-contained true

See Also

External Resources

Released under the MIT License.