Dotnet: Creating a solution

Carlos Costa

in brief, a dotnet solution is a container for projects, and it can be used to manage dependencies between them.

The first step is create a solutin file.

dotnet new sln -o MySolution

Here we are creating a solution file named MySolution. Our solution file will look like this:

# MySolution.sln

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal

A .sln file in dotnet contains information about the projects that are part of the solution.

Now we will add a project to our solution.

Using this command:

dotnet new list

We can see all the templates available to create a project. 👇

Template Name                                 Short Name           Language    Tags
--------------------------------------------  -------------------  ----------  --------------------------------
ASP.NET Core Empty                            web                  [C#],F#     Web/Empty
ASP.NET Core gRPC Service                     grpc                 [C#]        Web/gRPC
ASP.NET Core Web API                          webapi               [C#],F#     Web/WebAPI
ASP.NET Core Web App                          webapp,razor         [C#]        Web/MVC/Razor Pages
ASP.NET Core Web App (Model-View-Controller)  mvc                  [C#],F#     Web/MVC
ASP.NET Core with Angular                     angular              [C#]        Web/MVC/SPA
ASP.NET Core with React.js                    react                [C#]        Web/MVC/SPA
Blazor Server App                             blazorserver         [C#]        Web/Blazor
Blazor Server App Empty                       blazorserver-empty   [C#]        Web/Blazor/Empty
Blazor WebAssembly App                        blazorwasm           [C#]        Web/Blazor/WebAssembly/PWA
Blazor WebAssembly App Empty                  blazorwasm-empty     [C#]        Web/Blazor/WebAssembly/PWA/Empty
...

Let’s create a console app.

dotnet new console -o Console

The project need to be added to the solution.

dotnet sln add Console/Console.csproj

After this, we can see the project added to the solution file.

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Console", "Console\Console.csproj", "{6BF89819-700D-4115-A921-7F94D7FB8465}"

Now let’s create a data layer to our project to install the Entity Framework Core.

dotnet new classlib -o Data

Adding to our solution.

dotnet sln add Data/Data.csproj

First, we will add .NET Core EF CLI tools. See more

dotnet tool install --global dotnet-ef

Adding Entity Framework Core to our project.

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
#dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
#dotnet add package MySql.EntityFrameworkCore
#dotnet add package Microsoft.EntityFrameworkCore.Sqlite

A brief explanation of the packages:

  • EntityFrameworkCore: The main package, which is the ORM itself.
  • EntityFrameworkCore.Design: This package contains the design-time components of EF Core. It includes the EF Core commands for the Package Manager Console (PMC) in Visual Studio and the .NET Core CLI.
  • EntityFrameworkCore.SqlServer: This package contains SQL Server-specific extensions for EF Core.

In Console project, we will add a reference to Data project.

dotnet add Console reference Data

In Console.csproj, we can see the reference added.

<ProjectReference Include="..\Data\Data.csproj" />

Now do the same to add a test project.

dotnet new xunit -o Tests
dotnet sln add Tests/Tests.csproj
dotnet add Tests reference Data
dotnet add Tests reference Console

To finish, we will add a .gitignore file and editorconfig file.

dotnet new gitignore
dotnet new editorconfig

References