One of the technologies I don't cover in the other chapters of the book but is covered in the last chapter is Entity Framework Core. Today I'm showing is loosely taken from the section about EF Migrations.

Let's say you have a class called Race defined by the following lines of code

In order to work, EF Core needs to know the mapping between database tables, fields and properties of classes. The easiest approach, unless you want to write a lot of mapping code yourself, is to just stick to the default mapping conventions, and name tables and columns accordingly. And the best way of doing it, is to let EF generate the database for you. And this is done with a feature of EF called migrations.

Migrations can be used for the first setup of the database but more importantly later in the development when adding more features that require new tables or columns for existing tables.

To create the baseline migration for the class Race, you need to run the Add Migration command to generate the code to create the database schema (it gets stored in the Migrations folder), followed by the Update Database command to run this code on the database.

These two commands can be run either inside Visual Studio 2017, the Package Manager Console, or using the dotnet command-line tool.
In the first option type the following:

PM>Add-Migration Initial
PM>Update-Database

If you prefer using the dotnet command line, type these lines in the command prompt (but first you need to add the Microsoft.EntityFrameworkCore.Tools.DotNet package to the DotNetCliToolReference to the .csproj file).

>dotnet ef migration Initial
>dotnet ef database update

The table is created in the database configured in the appsettings.json file, under the ConnectionSettings section.

In the case of the Race class, the migration code generated is the following: