How to Use Migration Tool and Commands Entity Framework Core – Windows ASP.NET Core Hosting 2024 | Review and Comparison

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.

Data Access and Data Store

Every application generates and/or processes some data. The generated data or the processed data mostly are somehow persisted so that it can be used later by the same application or some other application.

For example, a web application might allow users to register for creating new account in the site. Then it might allow to add or update their personal information, etc.

Application uses data access code to interact with data stores. A data store is any place where this data is stored. Some examples can be flat file (technically possible), Mongo DBCosmosDB, a Sqllite databaseMySQL databaseSQL Server database, etc.

EF Core supports many database engines/data stores. Check documentation – Database Providers – for more details. Application connects to data store using connection string.

Best ASP.NET Hosting

Entity Framework Core Migrations

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

Traditionally, database creation, tables creation, seed data population is done using SQL scripts. Entity Framework core eliminates the need of SQL script for these tasks.

For defining a database, we can create a custom DbContext implementation, which will specify list of DbSets (a DbSet basically maps to table from relational database). DbSet is a generic type, it accepts a POCO class as type parameter. This POCO class can specify public properties and these properties would be columns of the SQL table. These set of classes is commonly referred to as EF Core Model.

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

Physically, when you generate migration script, a new C# file is generated. It’s name has date and timestamp signifying when this file was generated. This file has C# code to apply schema changes.

There are two ways to generate migrations

  • Using dotnet CLI tools (the recommended approach for .NET Core apps)
  • Using NuGet package manager console (for EF6 – .NET Framework like experience or for working from Visual Studio Package Manager)

In this article, we are going to have look at dotnet CLI tool and common commands to generate and apply migrations.

Install EF Core Tool

The dotnet CLI provides entity framework tool which can be used for generating migration. From below snippet, first command shows how to install the tool and second command is to update the already installed tool.

## To install the dotnet CLI ef tool
dotnet tool install --global dotnet-ef

## To update already existing tool
dotnet tool update --global dotnet-ef

## The project which contains EF Core Models
## Should have reference to this package
## For using EF Migration commands using above tool
dotnet add package Microsoft.EntityFrameworkCore.Design

Common Commands

This tool can be used for mainly purposes mentioned below:

  • To create a new migration– this is to generate new migration file
  • To remove an existing migration – this is to remove (deletes) the migration file.
  • To apply migrations to a database – this is equivalent of creating new or altering existing database
  • To remove existing database – this is equivalent of dropping existing database

Below code snippet shows all these commands. These commands can be very handy when we are working on creating a new database using entity framework.

## For Commands Given Below,
## SampleDbContext - is a class which is derived from DbContext

## To delete an existing database 
dotnet ef database update 0 --context SampleDbContext 

## To remove existing migrations 
dotnet ef migrations remove --context SampleDbContext 

## To create a new EF migration, with name InitialCreate
dotnet ef migrations add InitialCreate --context SampleDbContext

## To apply migrations to the database
## All tables specified in SampleDbContext would be created in the database
dotnet ef database update --context SampleDbContext

I hope you find this information useful. Let me know your thoughts.