EF Core Migrations

Effortlessly Evolving Database Schema with EF Core Migrations

Prabhashi Wijesinghe
LinkIT

--

Hi everyone!!!

I will share my experience on Migrations in Entity Framework Core with you all.

EF Core Migrations is a functionality in EF Core which facilitates the process of modifying the database schema in a controlled way that aligns with application changes. It allows developers to effortlessly apply or undo database modifications while retaining the existing data. With EF Core Migrations, you won’t have to create SQL scripts manually or be concerned about data loss since it generates and executes the scripts automatically.

Artwork by Author

Based on the diagram shown above, EF Core API constructs the EF Core model from the entity or domain classes, and EF Core migrations then generate or modify the database schema according to this model. It is essential to run the migration each time you make changes to the domain classes to ensure that the database schema stays current.

Why do you need Migrations?

EF Core Migrations is a practical and flexible approach to handling database modifications, especially in group development settings where multiple developers may be working on the same database schema. Here are some reasons why EF Core migrations are crucial:

  1. Schema evolution: EF Core migrations allow you to monitor and manage schema modifications over time as your application progresses.
  2. Consistent database state: EF Core migrations help maintain a consistent state across all database instances, minimizing the possibility of errors and inconsistencies within the database.
  3. Simplified database management: EF Core migrations offer a simplified approach to handling database modifications by allowing you to manage changes to the database in a versioned manner, which makes database management less complicated as your application progresses.
  4. Enhanced collaboration: EF Core migrations promote teamwork on a project by facilitating collaboration among developers. By allowing each developer to work with a local copy of the database that is up-to-date with the latest schema changes, EF Core migrations simplify collaboration and ensure a smoother workflow.
  5. Automated database creation: EF Core migrations can create the database schema automatically, negating the need for manual SQL scripts or other time-consuming database management processes.

Migration Commands in EF Core

EF Core has migrations enabled by default, which are managed through the execution of commands. If you are using Visual Studio, you can utilize the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to run Entity Framework CLI commands to create a migration.

The table below contains a list of significant migration commands in EF Core.

Creating a Migration

Initially, you have defined the classes for the domain of your application but you don’t have a database yet to store the data related to those classes. Therefore, the first step is to create a migration. To do so, open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and run the command to add a migration.

Package Manager Console
PM> add-migration FirstMigration

If you are utilizing the .NET Command Line Interface, then run the given command.

CLI
> dotnet ef migrations add FirstMigration

The given commands will create a migration named FirstMigration, which will result in the creation of three files in your project’s Migrations folder.

  • The main migration file in the format of “<timestamp>_<Migration Name>.cs” contains the migration operations that are defined in the Up() and Down() methods. The Up() method contains the code that is used to create database objects, whereas the Down() method contains the code that is used to remove database objects.
  • The “<timestamp>_<Migration Name>.Designer.cs” file is the metadata file for migrations that includes information used by EF Core.
  • The “<contextclassname>ModelSnapshot.cs” file represents a snapshot of your current model, which is used to determine the changes made when creating the next migration.

Once you have created a migration snapshot, the next step is to create the database.

Creating or Updating the Database

To create or update the database schema, you can use the following commands.

Package Manager Console
PM> update-Database
CLI
> dotnet ef database update

When you run the update command, the database will be created based on the context and domain classes, as well as the migration snapshot generated using the add-migration or add command.

In case this is the first migration, the command will create a table called __EFMigrationsHistory that will keep a record of all the applied migrations’ names on the database.

Removing a Migration

If the last created migration has not been applied to the database, you can remove it using the following commands.

Package Manager Console
PM> remove-migration
CLI
> dotnet ef migrations remove

These commands will remove the last created migration files and revert the model snapshot to the previous migration. However, it’s important to note that if the migration has already been applied to the database, these commands will throw an exception.

Reverting a Migration

Assuming that you have made changes to your domain class and created the SecondMigration using the add-migration command, followed by applying the changes to the database with the update command. However, if there’s a need to undo the changes and revert the database to its previous state, you can make use of the update-database <migration name> command. This command will enable you to rollback the database to a specific previous migration snapshot.

Package Manager Console
PM> Update-database FirstMigration
CLI
> dotnet ef database update FirstMigration

The commands mentioned above will revert the changes made to a database based on the migration named FirstMigration and remove all the changes applied by the SecondMigration migration. Furthermore, the SecondMigration entry in the __EFMigrationsHistory table of the database will be removed. However, it’s important to note that this command will not delete the migration files associated with SecondMigration from the project. You will need to use remove commands to remove them from the project.

Generating a SQL Script

To create a SQL script for the database, use the below commands.

Package Manager Console
PM> script-migration
CLI
> dotnet ef migrations script

By default, the script will include all migrations. However, you can specify a range of migrations to be included in the script by using the -to and -from options.

Thanks for taking the time to read the article. I appreciate your interest in EF Core Migrations and hope you found it helpful.

--

--

Prabhashi Wijesinghe
LinkIT
Writer for

A dedicated and hardworking IT undergraduate, willing to share and gain information related to IT industry👩‍💻