.NET Academy

dotnet CLI cheat sheet

Create, build, run, test, publish, packages, EF Core and user secrets.

SDK and templates

dotnet --version             dotnet --list-sdks          dotnet --info
dotnet new list              # available templates
dotnet new console -o MyApp  dotnet new webapi -o Api    dotnet new classlib -o Core
dotnet new xunit -o Tests    dotnet new web -o MinimalApi dotnet new blazor -o Ui
dotnet new gitignore         dotnet new editorconfig

Build, run, test

dotnet restore
dotnet build                          dotnet build -c Release
dotnet run                            dotnet run --project src/Api -- --port 5001
dotnet watch                          # rebuild and rerun on save
dotnet test                           dotnet test --filter "FullyQualifiedName~Orders"
dotnet test --collect:"XPlat Code Coverage"
dotnet clean

Solutions and references

dotnet new sln -n Shop
dotnet sln add src/Shop.Api tests/Shop.Tests
dotnet sln list
dotnet add src/Shop.Api reference src/Shop.Core

Packages (NuGet)

dotnet add package Serilog.AspNetCore
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 8.0.4
dotnet remove package Serilog.AspNetCore
dotnet list package                   dotnet list package --outdated
dotnet list package --vulnerable --include-transitive

Publish

dotnet publish -c Release -o ./publish
dotnet publish -c Release -r linux-x64 --self-contained          # includes the runtime
dotnet publish -c Release -r linux-x64 -p:PublishAot=true        # Native AOT
dotnet publish --os linux --arch x64 /t:PublishContainer         # container image, no Dockerfile

Secrets and certificates

dotnet user-secrets init
dotnet user-secrets set "ConnectionStrings:Shop" "Host=localhost;..."
dotnet user-secrets list              dotnet user-secrets remove "Key"
dotnet dev-certs https --trust        # trust the local HTTPS certificate
dotnet user-jwts create --role Admin  # test JWT for a protected API

EF Core tools

dotnet tool install --global dotnet-ef
dotnet ef migrations add AddOrders
dotnet ef migrations remove           # undo the last (unapplied) migration
dotnet ef database update             dotnet ef database update PreviousMigration
dotnet ef migrations script --idempotent -o migrate.sql
dotnet ef dbcontext scaffold "connection-string" Npgsql.EntityFrameworkCore.PostgreSQL

Diagnostics tools

dotnet tool install -g dotnet-counters dotnet-trace dotnet-dump dotnet-gcdump
dotnet-counters ps                    dotnet-counters monitor -p <pid>
dotnet-trace collect -p <pid>         dotnet-dump collect -p <pid>

Project file essentials

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
    <ProjectReference Include="..\Shop.Core\Shop.Core.csproj" />
  </ItemGroup>
</Project>