EF Core Migrations is a feature of Entity Framework Core (EF Core) that enables you to make changes to your model and automatically propagate those changes to your database schema. EF Core Migrations provide a way to incrementally apply schema changes to the database.
To use EF Core Migrations, you first need to install the Microsoft.EntityFrameworkCore.Design package and add a DbContext class to your project. Then, you can use the Add-Migration command in the Package Manager Console to create a new migration. This command will generate a migration class that contains up and down methods. The up method is used to apply the changes to the database, and the down method is used to revert the changes.
You can then use the Update-Database command to apply the pending migrations to the database. If you need to revert a migration, you can use the Update-Database command with the -Migration option to specify the migration to revert to.
EF Core Migrations are a convenient way to keep your database schema in sync with your model as your application evolves.When developing applications migration is a best way to keep the database schema in sync with the EF Core model by preserving data.
Migrations are enabled by default in EF Core and managed by executing commands. If you have Visual Studio 2019 or less then 2019, you can use the Package Manager Console to manage migrations.
In the above image, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model. Whenever you change the domain classes, you need to run migration to keep the database schema up to date.
Some of the must important migration commands in EF Core as given below.
PM Command | dotnet CLI Command | Usage |
add-migration <your Migration Name> | Add <Your migration name> | Creates a migration by adding a migration snapshot. |
Remove-migration | Remove | Removes the last migration snapshot. |
Update-database | Update | Removes the last migration snapshot. |
Script-migration | Add <migration name> | Removes the last migration snapshot. |
Creating a Migration
In the first state , you should defined the initial domain classes.There is no database for your application which can store the data from your domain classes. So, firstly, we need to create a migration as following command.
First open your 'Package Manager Console' as given image
PM> add-migration Initial
If you want to use dotnet Command Line Interface, execute the following command.
dotnet ef migrations add Initial
In the above commands, Initial is the name of a migration. This will create three files in the Migrations folder of your project, as shown below.
- 2020082..455_initial.cs: In the given above the main migration file which includes migration operations in the Up() and Down() methods. The Up() method includes the code for creating DB objects and Down() method includes code for removing DB objects.
- 2021022...5139_initial.Designer.cs: Int the above class name as migrations metadata file which contains information used by EF Core.
- DataContextModelSsnapshot.cs: In the given image above the migrations metadata file which contains information used by EF Core.
Updating the Database
PM> Update-Database
CLI
> dotnet ef database update
The Update command will create the table in the database which is based on the your DBContent and domain classes and the migration snapshot, which is created using the add-migration or add command.
Removing a Migration
We will use this command to remove the latest migration if it is not applied to the database.
PMC(Package Manager Console)
PM> remove-migration
CLI
> dotnet ef migrations remove
In the above commands they will remove the last migration and revert the model snapshot to the previous migration.
Reverting a Migration
PMC(Package Manager Console)
PM> Update-database MyFirstMigration
CLI
> dotnet ef database update MyFirstMigration.
In the given above command to reverse a migration which pass the name of a target migration to the update command. The target migration is the point to which you want to restore the database
Generating a SQL Script
PMC(Package Manager Console)
PM> script-migration
CLI
> dotnet ef migrations script
Comments