Migrations In Entity Framework Core

Tudip Technologies
2 min readApr 7, 2022

A Brief Intro About Entity Framework Core (EF Core)

Entity Framework Core is an ORM (object relational mapper) which can be used to access data from various data sources. Entity Framework was the initial framework which was part of classic .NET Framework (predecessor of .NET Core). Entity Framework Core is the open source, cross platform version of Entity Framework.

Entity Framework Core Migrations

Entity Framework Core provides a way to communicate with data stores using C# objects.

Earlier, database creation, tables creation, seed data population were done using SQL scripts. The need of scripts are eliminated by Entity Framework Core..

We can create a custom DbContext implementation for creating a database, that will specify list of DbSets (a DbSet basically maps to table from relational database). DbSet is generic type, it accepts POCO class as type parameter. This POCO class specify public properties and these properties are columns of the SQL table. These set of classes are commonly known as EF Core Model.

We can generate migrations using Entity Framework Core Tool. A migration is a script that is generated to keep the database in sync with the DbContext code. A migration is used to incrementally apply schema changes to database so as to keep database compatible with EF Core model.

When you generate the migration script, a new C# file is generated. It’s name has date and timestamp indicating when this file was generated. The file contains C# code to apply schema changes.

There are two ways to generate migrations

  • Using dotnet CLI tools (this is recommended approach for .NET Core apps)
  • By Using NuGet package manager console which is used for EF6 — .NET Framework like experience or for working from Visual Studio Package Manager.

In this blog, we are going to look at dotNET CLI tools and commands to generate and apply migrations.

Read more: https://tudip.com/blog-post/migrations-in-entity-framework-core/

--

--