EF Core Migrations
The data models change as features get implemented, new entities or properties are added and removed, and database schemas need to be changed accordingly to be kept in sync with the application.
In this article, we will go through the implementation of migration scripts in the following sections:
1. Creating Database and Tables
2. Modifying a Column in the Table
3. Executing SQL Commands
Let us start by Creating a Net Core Project
A. Creating .NetCore Project
Create a new Project named ‘EmployeeManagementSystem’
B. Configure the connection string in the AppSettings.json
C. Install Packages
Open Package Manager Console
Path: Tools > Nuget Package Manager > Package Manager Console
Run the following packages in the Package Manager Console
Install-Package Microsoft.EntityFrameworkCore -Version 6.0.3
Install-Package Microsoft.EntityFrameworkCore.Design -Version 6.0.3
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.3
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 6.0.3
D. Configure DBContext
Create a Context Class
Configure DbContext Service in Program.cs
E. Migrations: Database/Table Creation
- Adding a Model
We have completed Creating the Project and configuring the services.
Next, we will create a model named ‘Employee’ with the following fields:
- Id [Integer, Primary Key]
- Name [Varchar, Not Null]
The Employee Model has been configured. Now we need to generate a migration script based on the model created. To generate a migration script, the following command needs to be executed in the Package Manager Console.
Add-Migration <MigrationScriptName>
Once the Migration file has been generated, a new folder named ‘Migrations’ will be created under the Project. This folder will hold the generated migrations. The migration codes are generated based on the model created.
The screenshot below will display the generated migration code.
After generating the migration file, we need to execute the code, so that the model is synced with the database and its content. To execute the migration file, the following command needs to be executed in the Package Manager Console.
Update-Database
Once this command is executed, the EF will fetch the Connection string configured in the appSettings.json. If the configured database does not exist, a new database is created. Once the database is created the table ‘Employee’ will be created next.
Once the script has been executed successfully, an additional table will be generated named — ‘EFMigrationsHistory’. This table will log all the scripts executed. So, the next time when you execute a script, it checks if that script has already been executed or not. If executed, that script is excluded and the next script is taken for execution.
2. Modifying a Model
Now let's modify the Employee Model, by making the field ‘Name’ a Nullable string and adding a new integer field ‘Age’.
Generate script for the modified model.
Every time when a new migration script is generated, the application checks if there are any changes in the models, or if any new models are added. The migration codes are generated automatically based on these changes.
Now let's update these changes to the database:
3. Executing SQL Scripts
Next, let's create a migration to perform SQL Commands
Our models and the database are now synced. Let's try to create a migration script now.
Since there are no model updates, a blank migration file will be generated. If you have noticed. Each Migration file will be having 2 functions : up() & down().
The up() function is executed when the update database is triggered. The down() function is executed when we are removing a migration.
Let's write the code to perform to insert values to the employee table and also, let's create a stored procedure that accepts employee name and performs an insert.
Let us now perform the update-database. The result is displayed in the screenshot below.
F. Flow
- Add-Migration
2. Update-Database
Conclusion
We have gone through step by step implementation of EF Core Migration. FOr more information please go through the Microsoft documentation in this link.
I hope this article helped you and provided the confidence to take one step closer to implementing migrations in your project.
Happy Coding :)