Creating Applications Using .NET Core CLI

Introduction

 
Microsoft .NET Core CLI is a toolchain for developing cross-platform applications. If we are working in a Linux environment and wish to harness the power of .NET, we use Visual Studio Code along with .NET Core CLI. Also, it is helpful if we are working only with Visual Studio Code in Windows OS. Basically, in Visual Studio code, together with the terminal, we can create .NET Applications in any environment through .NET Core SDK and .NET Core Runtime. The .NET Core CLI is part of .NET Core SDK. Understanding CLI will help us work in Visual Studio Code in any Environment/OS. Let us focus on some commands to create a basic project. In this demo, we will create an application Skeletal by using Terminal in Visual Studio Code.
 
Tools/Software Used,
  1. .NET Core 3.1 SDK
  2. Visual Studio Code
Our final structure will look like this,
 
 

Application Development Steps

  • Create a solution. This solution will be named LibrarySolution. For this we will create a folder named LibrarySolution. Now, we will execute the command below,

    dotnet new sln

    This will create a file named LibrarySolution.sln automatically as per the folder name.
  • Create three projects. This structure will include 2 class libraries; i.e., LibrarySolution.Entities of .NET Standard Version 2.1, LibrarySolution.Repository of .NET Core 3.1 and ASP.NET Core MVC Project named LibrarySolution.App of .NET Core 3.1.
dotnet new classlib -f netstandard2.1 -o "LibrarySolution.Entities"
dotnet new classlib -f netcoreapp3.1 -o "LibrarySolution.Repository"
dotnet new mvc -f netcoreapp3.1 -o "LibrarySolution.App"
Verify that all the 3 projects exist.
  • Add all the projects to the solution one at a time.
dotnet sln add LibrarySolution.Entities/LibrarySolution.Entities.csproj
dotnet sln add LibrarySolution.Repository/LibrarySolution.Repository.csproj
dotnet sln add LibrarySolution.App/LibrarySolution.App.csproj
Verify in the Solution file. It will contain the name of all projects, shown below, 
 
 
 
  • Add a nuget package entity framework core to LibrarySolution.App and LibrarySolution.Repository.
dotnet add LibrarySolution.Repository/LibrarySolution.Repository.csproj package Microsoft.EntityFrameworkCore
dotnet add LibrarySolution.App/LibrarySolution.App.csproj package Microsoft.EntityFrameworkCore
Verify that the csproj file contains respective references.
  • Reference the Entities Project to Repository and App, Repository to App.
dotnet add LibrarySolution.Repository/LibrarySolution.Repository.csproj reference LibrarySolution.Entities/LibrarySolution.Entities.csproj
dotnet add LibrarySolution.App/LibrarySolution.App.csproj reference LibrarySolution.Entities/LibrarySolution.Entities.csproj
dotnet add LibrarySolution.App/LibrarySolution.App.csproj reference LibrarySolution.Repository/LibrarySolution.Repository.csproj
Verify that csproj file contains respective references.
  • Navigate to “LibrarySolution.App” folder in Terminal. Now, run the app.

    dotnet run
Verify that the application compiles successfully and runs correctly.
 
Our application will look like this in the Chrome browser,
 
 
Voila! That’s it! Now we have the basic structure ready and we can start using our coding skills to create amazing applications using C# in Visual Studio Code, all with the power of CLI and Visual Studio Code! Please share the best practices and ideas on how we can improve and be more productive. Thanks for reading this :)
 
Info: As a reference, all commands in .NET Core are available here.