Database Migration for .Net Core Projects
Let's take a look at a tutorial that explains how to implement a database migration for .Net Core projects.
Join the DZone community and get the full member experience.
Join For FreeYou can find all my .Net core posts here.
I am adding a new post after a long break because I recently joined a new company called AttachingIt. It is an awesome security-related company, and now, I am going to work on this awesome product.
In this post, I will explain how to do the Database migration for .Net Core projects.
Let us take an example and go step by step to complete the migration.
First Migration
If you are doing the migration for the first time, then once your DBContext is ready, open Powershell or Console and run the below command once you are in the root project:
dotnet ef migrations add InitialMigration -s ../MyProject.WebAPI -c DBContext -o Migrations
Here:
- -s is the source, WebAPI in our case
- -c is the Context, DBContext in our case, it is always good to mention context when we have multiple contexts
- -o is the Output path, Migrations folder in our case
Once you run these commands, it will create 3 new files under the migration folder.
For example:
- 00000000000000_InitialMigration.cs — This file is the main migrations file and contains the operations necessary to apply the migration (in Up()) and to revert it (in Down()).
- 00000000000000_InitialMigration.Designer.cs — This file is a migrations metadata file. It contains the information used by EF.
- DBContextModelSnapshot.cs — This file is a snapshot of our current model and is used to determine what changes are made when adding the next migration.
The timestamp in the filename helps keep them ordered chronologically so you can see the progression of changes.
The next step is to apply the migration to the database to create the schema. For that, run the below command in Console:
dotnet ef database update -s ../MyProject.WebAPI -c DBContext
That is it. Your changes should be reflected in the database.
Add New Migration
Adding a new migration has similar steps as the ones we just saw above.
For example, we want to remove a table from the database, and for that:
- Make sure you have removed all the dependencies for that table
- Remove the dependencies of that table from the DBContext of your application
- Open PowerShell or Console and run below command for adding new migration:
dotnet ef migrations add RemoveXTables -s ../MyProject.WebAPI -c DBContext -o Migrations
- Once you run these commands, it will create 2 new files under the migration folder and will update the DBContextModelSnapshot.cs
- Next step is to apply the migration to the database to create the schema. For that run below command:
The next step is to apply the migration to the database to create the schema. For that, run the below command:
dotnet ef database update -s ../MyProject.WebAPI -c DBContext
That is it. Your changes would be reflected in the database.
The same steps can be applied for any database related changes.
Revert the Migration
Sometimes, we need to revert the migration, and for that, we can use the same update command to apply migrations, but we need to specify the name of the migration you want to roll back to.
For example, if we want to revert back the migration to the initial migration, then:
dotnet ef database update InitialMigration -s ../MyProject.WebAPI -c DBContext
Note: Please note that it will just revert the database changes. It will not remove the migration files or update the snapshot file. You need to do it manually.
Remove the Migration
If you wish to remove your migration, then you can use the below command:
dotnet ef migrations remove
Revert All Migrations
To completely revert all migrations, do the following:
dotnet ef database update 0
Remove All Migrations
To completely remove all migrations and start all over again, do the following:
dotnet ef database update 0
dotnet ef migrations remove
SQL Script Generation
It is always good to have a script of the migration during the debugging of the migration or deploying the migration to higher environments.
Run the below command to generate the migration script:
dotnet ef migrations script -From <PreviousMigration> -To <LastMigration>
Here:
- From is the last migration that is applied to the database before running the script. If this is the first migration (no migration before), then just apply 0.
- To is the last migration that will be applied to the database after running the script.
- idempotent: This script only applies to the migrations if they haven't already been applied to the database.
Hope this helps.
Published at DZone with permission of Neel Bhatt, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments