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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Making Spring AI and OpenAI GPT Useful With RAG on Your Own Documents
  • Harmonizing Space, Time, and Semantics: Navigating the Complexity of Geo-Distributed IoT Databases
  • Accelerating Insights With Couchbase Columnar
  • Best Methods To Backup and Restore Database in SQL Server

Trending

  • KubeAdmiral: Next-Generation Multi-Cluster Orchestration Engine Based on Kubernetes
  • Mastering Backpressure in Java: Concepts, Real-World Examples, and Implementation
  • GitHub Shared Responsibility Model and Source Code Protection
  • Real-Time Remediation Solutions in Device Management Using Azure IoT Hub
  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.

Neel Bhatt user avatar by
Neel Bhatt
·
Aug. 08, 18 · Tutorial
Like (2)
Save
Tweet
Share
8.6K 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

  • Making Spring AI and OpenAI GPT Useful With RAG on Your Own Documents
  • Harmonizing Space, Time, and Semantics: Navigating the Complexity of Geo-Distributed IoT Databases
  • Accelerating Insights With Couchbase Columnar
  • Best Methods To Backup and Restore Database in SQL Server

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • 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: