Categories
Cheat Sheets Tips

dotnet cli cheat sheet

The .NET command-line interface (CLI) is a cross-platform toolchain for developing, building, running, and publishing .NET applications.

The .NET CLI is included with the .NET SDK. To learn how to install the .NET SDK, see Install .NET Core.

Full documentation is available at Microsoft’s Docs, this blog post is an overview of the most commonly used CLI commands.

dotnet --info

To display information about installed SDKs and Runtimes.

dotnet new sln

To create an empty solution. If you don’t provide the name of the solution it will use directory’s name instead and create a solution file.

dotnet new webapi -o api

Create a new WebApi template project and output in to api folder, -o parameter is used to specify the output directory name

donet sln add .\api\

Add the project to the solution. If you just specify the name of the folder it will go and find all the projects inside that folder and add them to the solution.

dotnet sln list

It will list all the projects inside the solution. You must be in the root folder where .sln file exists.

code .

To open Visual Studio Code in the current directory.

VS Code will ask you to generate debug assets, click ‘Yes’, if you accidentally miss to click ‘Yes’ you can press Ctrl + Shift + P and type Generate Assets for Build and Debug.

cd api
dotnet run

Go to your Api folder and run the api project

dotnet watch run

It will keep an eye on the files changed and automatically recompile the app.

dotnet restore

Will restore the nuget packages.

If you try to access the https endpint it will generate some warnings/errors regarding the self-signed certificate. In the Postman (or similar) you can disable the cert verification (it will give you an option if you make a request to https endpoint) or you can trust your dev cert by running these commands:

dotnet dev-certs https

Will generate a cert for you or if it is already present it will confirm it’s presence but that does not mean that your OS trusts the cert.

dotnet dev-certs https -t

On windows it will show a warning message and if you select Yes it will entrust the self signed cert.

dotnet dev-certs https --clean

Delete the local cert for development – you will be asked for confirmation

dotnet dev-certs https -t

Create and trust new cert.


Generate & Run Migrations

Install entity framework migration tool, you need to check the runtime version to install entity framework core nuget package. To check the latest runtime version installed on your machine run:

dotnet --info

and then run:

dotnet tool install --global dotnet-ef --version 3.1.9

Install nuget package:
Microsoft.EntityFrameworkCore.Design

dotnet ef -h

To list supported commands

dotnet ef migrations add InitialCreate -o Data/Migrations

Add new migration with output folder specified

dotnet ef database update
dotnet ef database drop -p Infrastructure -s api

Drops the database

dotnet ef migrations remove -p Infrastructure -s api

Remove migrations where infrastructure is the class library having entity framework nuget packages installed.

dotnet ef migrations add InitialiCreate -p Infrastructure -s api -o Data/Migrations

Creates the initial migration inside Data/Migrations folder in Infrastructure class library. (That’s where the dbContext lives)


Test, Build and Release

dotnet build --configuration Debug

Build the application under debug configuration. We can also pass in –configuration Release to build the application for release.

dotnet publish --no-build --configuration Debug --output /folder/of/your/choice

Publish the build to the /tmp directory, we can pass in –configuration Release to build the application for release.

dotnet test --configuration Release --no-build

dotnet test command to run the unit tests

dotnet test Tailspin.SpaceGame.Web.Tests --configuration Release --no-build --logger trx

Provide the –logger option to write the results to a log file

That’s all folks! Happy coding.