In this post, I will discuss, How migration process works in Entity Framework Core. A deep insight into the EF Core migration process will be given in this blog.
Based on the changes in the model, the Migration automatically updates the database schema. A sync between the EF core and database schema is always maintained this way. This will be followed by a process in which the system will build a query that will work with our database schema and it will also process the data that returns from the database in order to make an object from it.
Let’s get the migration started:
Installation of Microsoft.EntityFrameworkCore.Tools is the first step
The following commands are available via the Microsoft.EntityFrameworkCore.Tools package if you switch to the ConsoleApp project.
My executable project is a ConsoleApp, so I should install this package there
Add – Migration Command:
A migration and snapshot file are generated by the Add-Migration command. Enter the following command in the package manager console
Migration names can be any name, the Initial is an example.
EFCoreDemo.Data should be the default project in the package manager console, and ConsoleApp should be your start-up project.
Examining the migration process:
Adding a migration creates a migration folder that contains two files, as shown in the above figure
- Model snapshot file – This file contains the current status of the model. As soon as the migration is added, EF core will compare the existing snapshot with the migration file and generate a query based on the new snapshot.
- In the migration file, the migration commands describe what needs to be done in the database. A table can be created using the Up method and a table can be deleted using the Down method.
Execute the following command again in the package manager console
It creates a table in the database based on the migration script, if the database does not exist it creates one and runs the migration script.
Script-Migration generates the consolidate script based on the model for production environments; that script should be used in production databases to execute.
Here are some things you learned from this blog:
- A workflow for determining the database schema by the EF Core
- Integration API for migrations
- To create and execute a migration, use PowerShell
- Script generation for production using the migration command
- This blog has hopefully been enjoyable for you. If you have questions, comments, or feedback regarding this blog, please do not hesitate to contact me.