Entity Framework Core Migration

DZone 's Guide to

Entity Framework Core Migration

When you think 'front-end development,' you don't always think 'data,' but knowing how to migrate data into and out of your app is crucial.

· Web Dev Zone ·
Free Resource

In the last chapter of my upcoming book "Front-end Development with ASP.NET Core, Angular, and Bootstrap" I build a very simple application to show how all the various technologies play together, and also to show some features that are not specific to front-end development but are needed in a real-world application.

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. What I'm showing today 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

public class Race
    public int ID { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public DateTime Date { get; set; }

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 theAdd-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 .NET command-line tool. In the first option type the following:

PM>Add-Migration Initial 

If you prefer using the .NET command line, type these lines in the command prompt (but first you need to add the Microsoft.EntityFrameworkCore.Tools.DotNet package to the DotNetCliToolReference in 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:

    name: "Race",
    columns: table => new
        ID = table.Column<int>(type: "int", nullable: false)
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
        Date = table.Column<DateTime>(type: "datetime2", nullable: false),
        Location = table.Column<string>(type: "nvarchar(max)", nullable: true),
        Name = table.Column<string>(type: "nvarchar(max)", nullable: true)
    constraints: table =>
        table.PrimaryKey("PK_Race", x => x.ID);

And the following mapping code:

modelBuilder.Entity("TriathlonRaceTracking.Models.Race", b =>

Tomorrow I'll continue covering Entity Framework Core migrations.

data migration, entity framework core, web application development, web dev

Published at DZone with permission of Simone Chiaretta , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}