DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?

Trending

  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • Navigating the LLM Landscape: A Comparative Analysis of Leading Large Language Models
  • How AI Is Changing the Way Developers Write Code
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  1. DZone
  2. Data Engineering
  3. Databases
  4. Database Migration for .Net Core Projects

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.

By 
Neel Bhatt user avatar
Neel Bhatt
·
Aug. 08, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.1K Views

Join the DZone community and get the full member experience.

Join For Free

You 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:

  1. Make sure you have removed all the dependencies for that table
  2. Remove the dependencies of that table from the DBContext of your application
  3. Open PowerShell or Console and run below command for adding new migration:
dotnet ef migrations add RemoveXTables -s ../MyProject.WebAPI -c DBContext -o Migrations
  1. Once you run these commands, it will create 2 new files under the migration folder and will update the DBContextModelSnapshot.cs
  2. 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.

Database

Published at DZone with permission of Neel Bhatt, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!