Command Line Interface (CLI) is a very popular tool is almost all popular frameworks like Yeoman Generator, Angular, and others. It lends developers access to execute commands to create, build, and run projects, restore packages, and so on.
.NET CLI provides a toolset with a handful commands that can be executed from the command line interface to create .NET Core projects, restore dependencies, and build and run projects. Under the wire, Visual Studio 2015/2017 and Visual Studio Code even uses this tool to perform different options taken by the developers from their IDE; for example, to create a new project using .NET CLI, we can run the following command:
dotnet new
It will list down the available templates and the short name that can be used while creating the project.
Here is the screenshot containing the list of project templates that can be used to create/scaffold projects using .NET Core CLI:
And by running the following command, a new ASP.NET Core MVC application will be created:
dotnet new mvc
The following screenshot shows the provisioning of the new MVC project after running the preceding command. It creates the project in the same directory where the command is running and restores all the dependencies:
To install the .NET Core CLI toolset, there are some native installers available for Windows, Linux, and macOS. These installers can install and set up the .NET CLI tooling on your machine and developers can run the commands from the CLI.
Here is the list of commands with their descriptions that are provided in the .NET Core CLI:
Command
|
Description
|
Example
|
new
|
Creates a new project based on the template selected
|
dotnet new razor
|
restore
|
Restores all the dependencies defined in the project
|
dotnet restore
|
build
|
Builds the project
|
dotnet build
|
run
|
Runs the source code without any additional compile
|
dotnet run
|
publish
|
Packages the application files into a folder for deployment
|
dotnet publish
|
test
|
Used to execute unit tests
|
dotnet test
|
vstest
|
Executes unit tests from specified files
|
dotnet vstest [<TEST_FILE_NAMES>]
|
pack
|
Packs the code into a NuGet package
|
dotnet pack
|
migrate
|
Migrates .NET Core preview 2 to .NET Core 1.0
|
dotnet migrate
|
clean
|
Cleans the output of the project
|
dotnet clean
|
sln
|
Modifies a .NET Core solution
|
dotnet sln
|
help
|
Displays the list of commands available to execute through .NET CLI
|
dotnet help
|
store
|
Stores the specified assemblies in the runtime package store
|
dotnet store
|
Here are some of the project level commands that can be used to add a new NuGet package, remove an existing one, list references, and others:
Command
|
Description
|
Example
|
add package
|
Adds a package reference to the project
|
dotnet add package Newtonsoft.Json
|
remove package
|
Removes a package reference from the project
|
dotnet remove package Newtonsoft.Json
|
add reference
|
Adds a project reference to the project
|
dotnet add reference chapter1/proj1.csproj
|
remove reference
|
Removes the project reference from the project
|
dotnet remove reference chapter1/proj1.csproj
|
list reference
|
List down all the project references in the project
|
dotnet list reference
|
The following are some common Entity Framework Core commands that can be used to add migration, remove migration, update the database, and so on.
Command |
Description |
Example |
dotnet ef migrations add |
Adds a new migration |
dotnet ef migrations add Initial
- Initial is the name of migration |
dotnet ef migrations list |
List available migrations |
dotnet ef migrations list |
dotnet ef migrations remove |
Remove specific migration |
dotnet ef migrations remove Initial
- Initial is the name of migration |
dotnet ef database update |
To update the database to a specified migration |
dotnet ef database update Initial
- Initial is the name of migration |
dotnet ef database drop |
Drops the database |
dotnet ef database drop |
Here are some of the server level commands that can be used to delete the NuGet package from its actual source repository from the machine, add NuGet package into its actual source repository on the machine, and so on:
Command
|
Description
|
Example
|
nuget delete
|
Deletes the package from the server
|
dotnet nuget delete Microsoft.AspNetCore.App 2.0
|
nuget push
|
Pushes a package to the server and publishes it
|
dotnet nuget push foo.nupkg
|
nuget locals
|
Lists the local NuGet resources
|
dotnet nuget locals -l all
|
msbuild
|
Builds a project and all of its dependencies
|
dotnet msbuild
|
dotnet install script
|
The script to install the .NET CLI tools and the shared runtime
|
./dotnet-install.ps1 -Channel LTS
|
To run the preceding commands, we can use the tool known as dotnet from the command line and specify the actual command followed by that. When the .NET Core CLI is installed, it is set into the PATH variable in Windows OS and can be accessed from any folder. So, for example, if you are at your project root folder and wanted to restore the dependencies, you can just call the following command and it will restore all the dependencies that have been defined in your project file:
dotnet restore
The preceding command will start restoring the dependencies or project-specific tools, as defined in the project file. The restoration of tools and dependencies are done in parallel:
We can also set the path where packages can be restored by using the --packages argument. However, if this is not specified, it uses the .nuget/packages folder under the system's user folder. For example, the default NuGet folder for Windows OS is {systemdrive}:\Users\{user}\.nuget\packages and /home/{user} for Linux OS, respectively.