.NET Core CLI: Essential Commands for Developers

Spread the love

The .NET Core Command-Line Interface (CLI) is a powerful tool for developers who is working with .NET Core applications. Whether you are building, testing or deploying your projects, the .NET Core CLI provides a streamlined way to manage your workflow.

In this article, I will explore some of the most essential .NET Core CLI commands that every developer should know.

What is the .NET Core CLI?

The .NET Core CLI is a cross-platform toolchain that allows developers to create, build, run and publish .NET Core applications from the command line. It is a key component of the .NET ecosystem for enabling developers to work efficiently without relying on Visual Studio interface.

Getting Started with .NET Core CLI

Before diving into the commands you need to ensure you have the .NET SDK installed on your machine. You can check your installation by running:

dotnet --version

This command will display the installed .NET SDK version. If it is not installed, download it from the official .NET website.

Essential .NET Core CLI Commands

1. Create a New Project

To start a new .NET Core project by using the new command. This command scaffolds a new project based on the specified template.

dotnet new <template> -o <output_directory>

For example, to create a new console application:

dotnet new console -o MyConsoleApp

Common templates include:

  • console: Console application
  • web: ASP.NET Core web application
  • mvc: ASP.NET Core MVC application
  • classlib: Class library

2. Build a Project

The build command compiles your project and its dependencies into binary files.

dotnet build

This command is essential for ensuring your code compiles successfully before running or publishing it.

3. Run a Project

To execute your application, use the run command.

dotnet run

This command builds and runs your project in one step by making it ideal for quick testing during development.

4. Add a Package or Reference

To add a NuGet package or project reference by using the add command.

dotnet add package <package_name>

For example, to add the Newtonsoft.Json package:

dotnet add package Newtonsoft.Json

To add a project reference:

dotnet add reference <path_to_project>

 5. Restore Dependencies

The restore command downloads all the dependencies specified in your project file.

dotnet restore

This command is automatically run by build and run, but you can use it explicitly if needed.

6. Test Your Project

To run unit tests, use the test command.

dotnet test

This command executes tests defined in your project using a test framework like xUnit, NUnit, or MSTest.

7. Publish a Project

The publish command packages your application for deployment.

dotnet publish -c Release -o <output_directory>

This command generates a self-contained or framework-dependent deployment of your application, ready to be deployed to a server or container.

8. Manage Migrations (Entity Framework Core)

If you are using Entity Framework Core, you can manage database migrations with the following commands:

Add a new migration:

dotnet ef migrations add <migration_name>

Update the database:

dotnet ef database update

 9. Clean the Project

The clean command removes build outputs and temporary files.

dotnet clean

This command is useful for freeing up disk space or resolving build-related issues.

10. List Installed SDKs and Runtimes

To check the installed .NET SDKs and runtimes, use the –list-sdks and –list-runtimes options.

dotnet --list-sdks 
dotnet --list-runtimes

These commands help you verify your development environment setup.

Tips for Using the .NET Core CLI

  • Use –help for Command Details: Append –help to any command to see its usage and options. For example:
dotnet build --help
  •  Leverage Global Tools: The .NET Core CLI supports global tools that extend its functionality. Install tools like dotnet-ef or dotnet-reportgenerator to enhance your workflow.
  • Automate Tasks with Scripts: Combine CLI commands into scripts to automate repetitive tasks, such as building, testing, and deploying your application.

In conclusion, the .NET Core CLI is an indispensable tool for modern .NET developers. By mastering these essential commands, you can streamline your development process, improve productivity, and take full advantage of the .NET ecosystem. Whether you’re a beginner or an experienced developer, the CLI empowers you to work efficiently across platforms and environments.

For more .NET tips and tutorials, stay tuned to TheDotNetWay!


Spread the love

Leave a Comment