Command-Lines
To make it easier to enter commands at the prompt, this page lists all commands as a single line that can be copied and pasted.
- Chapter 1 - Hello, C#! Welcome, .NET!
- Page 9 - Managing Visual Studio Code extensions at the command line
- Page 14 - Listing and removing versions of .NET
- Page 27 - Writing code using Visual Studio Code
- Page 29 - Compiling and running code using the dotnet CLI
- Page 36 - Cloning the book solution code repository
- Page 36 - Getting help for the dotnet tool
- Chapter 2 - Speaking C#
- Chapter 3 - Controlling Flow, Converting Types, and Handling Exceptions
- Chapter 7 - Packaging and Distributing .NET Types
- Page 315 - Checking your .NET SDKs for updates
- Page 324 - Creating a .NET Standard 2.0 class library
- Page 325 - Controlling the .NET SDK
- Page 328 - Creating new projects
- Page 330 - Publishing a self-contained app
- Page 332 - Publishing a single-file app
- Page 333 - Enabling assembly-level trimming
- Page 333 - Enabling type-level and member-level trimming
- Page 350 - .NET Upgrade Assistant
- Chapter 10 - Working with Data Using Entity Framework Core
- Chapter 11 - Querying and Manipulating Data Using LINQ
- Chapter 12 - Introducing Web Development Using ASP.NET Core
- Chapter 13 - Building Websites Using ASP.NET Core Razor Pages
- Chapter 14 - Building Websites Using the Model-View-Controller Pattern
- Chapter 15 - Building and Consuming Web Services
- Chapter 16 - Building User Interfaces Using Blazor
code --install-extension ms-dotnettools.csharp
Listing all installed .NET SDKS:
dotnet --list-sdks
Listing all installed .NET runtimes:
dotnet --list-runtimes
Details of all .NET installations:
dotnet --info
Remove all but the latest .NET SDK preview:
dotnet-core-uninstall remove --all-previews-but-latest --sdk
Creating a new Console App project:
dotnet new console
Creating a new Console App project that targets a specified framework version, for example, .NET 6:
dotnet new console -f net6.0
Creating a new Console App project that in a named subfolder:
dotnet new console -o HelloCS
dotnet run
git clone https://github.com/markjprice/cs11dotnet7.git
Getting help for a dotnet
command like new
:
dotnet help new
Getting help for a specified project template, for example, console
:
dotnet new console -h
dotnet --version
Running a project with its release configuration:
dotnet run --configuration Release
Running a project with its debug configuration:
dotnet run --configuration Debug
Adding the Microsoft.Extensions.Configuration.Binder
package:
dotnet add package Microsoft.Extensions.Configuration.Binder
Adding the Microsoft.Extensions.Configuration.Json
package:
dotnet add package Microsoft.Extensions.Configuration.Json
Note: You do not need to add the other two packages because they are transitive depedencies and so will be implicitly referenced.
dotnet sdk check
dotnet new classlib -f netstandard2.0
Creating a global.json
file to control to default .NET SDK for projects created in the current folder and its descendents:
dotnet new globaljson --sdk-version 6.0.404
Listing available project templates using .NET 7:
dotnet new list
Listing available project templates using .NET 6:
dotnet new --list
Listing available project templates using .NET 6 short form:
dotnet new -l
Build and publish the release version for Windows:
dotnet publish -c Release -r win10-x64 --self-contained
Build and publish the release version for macOS on Intel:
dotnet publish -c Release -r osx-x64 --self-contained
Build and publish the release version for macOS on Apple Silicon:
dotnet publish -c Release -r osx.11.0-arm64 --self-contained
Build and publish the release version for Linux on Intel:
dotnet publish -c Release -r linux-x64 --self-contained
Build and publish the release version for Linus on ARM64:
dotnet publish -c Release -r linux-arm64 --self-contained
dotnet publish -c Release -r win10-x64 --no-self-contained /p:PublishSingleFile=true
dotnet publish -c Release -r win10-x64 --self-contained /p:PublishSingleFile=true
dotnet publish -c Release -r win10-x64 --self-contained /p:PublishSingleFile=true -p:PublishTrimmed=True
dotnet publish -c Release -r win10-x64 --self-contained /p:PublishSingleFile=true -p:PublishTrimmed=True -p:TrimMode=Link
Installing the .NET Upgrade Assistant:
dotnet tool install -g upgrade-assistant
Creating the Northwind SQLite database:
sqlite3 Northwind.db -init Northwind4SQLite.sql
Listing installed dotnet
global tools:
dotnet tool list --global
Uninstalling an older dotnet-ef
tool:
dotnet tool uninstall --global dotnet-ef
Installing the latest dotnet-ef
as a global tool:
dotnet tool install --global dotnet-ef
dotnet ef dbcontext scaffold "Filename=Northwind.db" Microsoft.EntityFrameworkCore.Sqlite --table Categories --table Products --output-dir AutoGenModels --namespace WorkingWithEFCore.AutoGen --data-annotations --context Northwind
Note the following:
- The command action:
dbcontext scaffold
- The connection string:
"Filename=Northwind.db"
- The database provider:
Microsoft.EntityFrameworkCore.Sqlite
- The tables to generate models for:
--table Categories --table Products
- The output folder:
--output-dir AutoGenModels
- The namespace:
--namespace WorkingWithEFCore.AutoGen
- To use data annotations as well as the Fluent API:
--data-annotations
- To rename the context from [database_name]Context:
--context Northwind
Creating the Northwind SQLite database:
sqlite3 Northwind.db -init Northwind4Sqlite.sql
Creating the Northwind SQLite database:
sqlite3 Northwind.db -init Northwind4SQLite.sql
Creating the EF Core model for the Northwind database:
dotnet ef dbcontext scaffold "Filename=../Northwind.db" Microsoft.EntityFrameworkCore.Sqlite --namespace Packt.Shared --data-annotations
Creating the EF Core model for the Northwind database:
dotnet ef dbcontext scaffold "Data Source=.;Initial Catalog=Northwind;Integrated Security=true;TrustServerCertificate=true;" Microsoft.EntityFrameworkCore.SqlServer --namespace Packt.Shared --data-annotations
If you do not have a "full" edition of SQL Server installed with a default instance then you will need to changed the
.
to the correctservername\instancename
.
Starting an ASP.NET Core project and specifying the https
profile:
dotnet run --launch-profile https
dotnet new razorclasslib --support-pages-and-views
Showing all the options for an ASP.NET Core MVC project:
dotnet new mvc --help
dotnet ef database update
Creating a Web API project using Minimal APIs:
dotnet new webapi --use-minimal-apis
Creating a Web API project using Minimal APIs short form:
dotnet new webapi -minimal
Creating a Blazor WASM project that supports Progressive Web App capabilities and is hosted in an ASP.NET Core website project:
dotnet new blazorwasm --pwa --hosted