You can find my all .Net core posts here.
I am adding new post after a long break because I recently joined a new company AttachingIt. It is an awesome security related company and now-on wards 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 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 main migrations file. This file 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. This file Contains the information used by EF.
- DBContextModelSnapshot.cs— This file is a snapshot of our current model. This is Used to determine what are the changes made when adding the next migration.
The timestamp in the filename helps keep them ordered chronologically so you can see the progression of changes.
Next step is to apply the migration to the database to create the schema. For that run below command in Console:
dotnet ef database update -s ../MyProject.WebAPI -c DBContext
That is it. Your changes would be reflected in the database.
Add new migration
Adding new migration has similar steps as we just saw above.
For example, we want to remove a table from the database, 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:
dotnet ef database update -s ../MyProject.WebAPI -c DBContext
That is it. Your changes would be reflected in the database.
Same steps can be applied for any database related changes.
Revert the migration
Sometimes we need to revert the migration, 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 below command:
For example, if we want to remove the last migration then run below command:
dotnet ef migrations remove
Revert all migration
To completely revert all migrations, do the following:
dotnet ef database update 0
Remove all migration
To completely remove all migrations and start all over again, do the following:
dotnet ef database update 0
dotnet ef migrations remove
SQL Scrip generation
It is always good to have a script of the migration during the debugging of migration or deploying the migration to higher environments.
Run below command to generate the migration script:
dotnet ef migrations script -From <PreviousMigration> -To <LastMigration>
Here:
- From is the last migration which 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 which we will be applied to the database after running the script
- idempotent : This script only applies migrations if they haven’t already been applied to the database.
Hope it helps.
You can find my all .Net core posts here.
You can also use Migrator.net for migrations in net core. I’ve created a Version with net core support: https://github.com/dotnetprojects/Migrator.NET
LikeLike
Okay. Thanks a lot for the information.
LikeLike