Database Migrations in Entity Framework Core

 ๐Ÿ”„ Database Migrations in Entity Framework Core

Entity Framework Core (EF Core) is a popular Object-Relational Mapper (ORM) for .NET applications. It allows developers to work with a database using .NET objects and handles database schema changes through a feature called migrations.


What Are Migrations?

Migrations are a way to incrementally update your database schema to keep it in sync with your application's data model (your C# classes).


Why Use Migrations?

Version control your database schema changes.


Safely apply schema updates to development, testing, and production environments.


Avoid manual database updates and reduce errors.


Easily rollback changes if necessary.


How to Use Migrations in EF Core

1. Set Up Your DbContext and Models

Define your entity classes and DbContext:


csharp

Copy

Edit

public class ApplicationDbContext : DbContext

{

    public DbSet<Book> Books { get; set; }

}


public class Book

{

    public int Id { get; set; }

    public string Title { get; set; }

    public string Author { get; set; }

}

2. Add a Migration

From the command line (in your project directory), run:


bash

Copy

Edit

dotnet ef migrations add InitialCreate

This creates a migration file with code to create the database schema matching your models.


The migration file is stored under the Migrations/ folder.


3. Apply the Migration to the Database

Apply the migration to your database by running:


bash

Copy

Edit

dotnet ef database update

This command applies all pending migrations and creates/updates the database schema.


4. Making Changes

If you update your models, for example by adding a new property:


csharp

Copy

Edit

public class Book

{

    public int Id { get; set; }

    public string Title { get; set; }

    public string Author { get; set; }

    public DateTime PublishedDate { get; set; }  // New field

}

Run:


bash

Copy

Edit

dotnet ef migrations add AddPublishedDateToBook

dotnet ef database update

This creates a new migration to add the PublishedDate column and updates the database.


Common Migration Commands

Command Purpose

dotnet ef migrations add <name> Create a new migration based on model changes

dotnet ef database update Apply pending migrations to the database

dotnet ef migrations remove Remove the last migration (if not applied yet)

dotnet ef migrations list List all migrations


Tips

Use Source Control to keep migration files in version control.


Always test migrations in a development or staging environment before applying to production.


For production, generate SQL scripts for migrations using:


bash

Copy

Edit

dotnet ef migrations script

Use these scripts for review and manual database updates if needed.


✅ Summary

Entity Framework Core migrations enable smooth and controlled database schema evolution by:


Tracking model changes


Generating migration files


Applying schema updates to databases


Simplifying database version management

Learn Full Stack Dot NET Training in Hyderabad

Read More

Entity Framework Core: The ORM for Full Stack .NET Developers

Working with NoSQL Databases in .NET (MongoDB, Redis)

Setting Up a SQL Server Database in Full Stack .NET Applications

Introduction to SQL Server for .NET Developers

Visit Our Quality Thought Training in Hyderabad

Get Directions


Comments

Popular posts from this blog

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Installing Tosca: Step-by-Step Guide for Beginners

Why Data Science Course?