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 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
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  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
CORE ·
Aug. 08, 18 · Tutorial
Like (2)
Save
Tweet
Share
8.11K 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.

Popular on DZone

  • The Beauty of Java Optional and Either
  • DeveloperWeek 2023: The Enterprise Community Sharing Security Best Practices
  • 5 Common Firewall Misconfigurations and How to Address Them
  • An End-to-End Guide to Vue.js Testing

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
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: