Entity Framework Tutorial: How Can I Utilize EF Core Migrations?

Keeping the database schema in line with the data model of your application is one of the most frequent jobs when working with Entity Framework Core (EF Core). Herein lies the role of migration. Every time your data model changes, you can update your database structure (tables, columns, and relationships) in EF Core without erasing any current data thanks to migrations.

In this post, we’ll define migrations in EF Core, discuss their significance, and provide step-by-step instructions with straightforward examples. Anyone looking to learn how to use EF Core migrations or EF Core migration commands will find this article helpful as it is also SEO-friendly.

What Are EF Core Migrations?

Migrations in EF Core are a way to apply changes in your C# data model classes to your database. Instead of manually editing the database, migrations generate scripts to add, remove, or modify database objects.

  • Purpose: Keeps your database schema in sync with your application model.
  • Benefit: Automates changes without deleting existing data.
  • Example Scenario: If you add a new property Age to your User class, you can run a migration to add the new Age column to the Users table in the database.

How to Enable Migrations in EF Core?

Before creating migrations, make sure EF Core tools are installed.

  1. Open the Package Manager Console in Visual Studio or use CLI (Command Line Interface).
  2. Install the EF Core tools package if not already done:
  1. Add EF Core design package to your project:

This prepares your project to use migrations.

How to Create a Migration in EF Core

To create your first migration, run this command in your project folder:

  • InitialCreate is just a name for your migration. You can choose any name.
  • This command generates migration files inside the Migrations folder.

Example

If your User class looks like this:

After running dotnet ef migrations add InitialCreate, EF Core will create a migration to build a Users table with Id and Name columns.

How to Apply Migrations to the Database

Once you have created a migration, you need to apply it to the database using:

  • This command updates the database based on the migration files.
  • Example: If you added a new column Age, running this command will add that column to the Users table.

Updating a Migration in EF Core

When your data model changes, you can create another migration.

Example

Now run

This will generate a migration to add an Age column to the Users table. Then apply it with:

How to Remove or Rollback a Migration

Sometimes you may need to undo or remove a migration.

  • To remove the last migration before applying it:
  • To rollback to a specific migration:

Example

This command will rollback the database to the state after the InitialCreate migration.

Benefits of Using EF Core Migrations

  • Keeps models and database in sync automatically.
  • Saves time by generating scripts instead of manual SQL changes.
  • Safe updates without losing data.
  • Version control friendly, as migrations can be committed with code.
  • Easier teamwork, since all developers can share the same migration history.

Summary

Migrations in EF Core are a powerful feature to keep your C# data models and database schema in sync. With simple commands like dotnet ef migrations add and dotnet ef database update, you can add new tables, modify columns, and manage changes without writing SQL manually. By learning how to use migrations in EF Core, developers can ensure smooth database updates and maintain consistency across projects.

ASP.NET 10.0 Hosting Recommendation

ASP.NET is a powerful platform for creating web applications and services. You must be comfortable with JavaScript, HTML, CSS, and C# before developing a web application in ASP.NET. On the market, there are thousands of web hosting companies providing ASP.NET Hosting. But, only very few web hosting companies could provide high quality ASP.NET hosting solution.

ASP.NET is the best development language in Windows platform, which is released by Microsoft and widely used to build all types of dynamic Web sites and XML Web services. With this article, we’re going to help you to find the best ASP.NET Hosting solution in Europe based on reliability, features, price, performance and technical support. After we reviewed about 30+ ASP.NET hosting providers in Europe, our Best ASP.NET Hosting Award in Europe goes to HostForLIFE.eu, one of the fastest growing private companies and one of the most reliable hosting providers in Europe.

You may also like...

Popular Posts