---
title: "How to handle database migrations and schema changes in an ASP.NET MVC project?"  
description: "How to handle database migrations and schema changes in an ASP.NET MVC project?"  
author: "Revati S Misra"  
published: 2023-06-11  
updated: 2023-11-18  
canonical: https://www.mindstick.com/forum/158715/how-to-handle-database-migrations-and-schema-changes-in-an-asp-dot-net-mvc-project  
category: "asp.net mvc"  
tags: ["asp.net mvc", "database connection", "database schema"]  
reading_time: 3 minutes  

---

# How to handle database migrations and schema changes in an ASP.NET MVC project?

How to [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) [database migrations](https://www.mindstick.com/forum/160251/explain-the-purpose-of-database-migrations-in-entity-framework-core) and [schema changes](https://www.mindstick.com/forum/160211/how-to-handle-database-migrations-and-schema-changes-in-a-dot-net-core-api-using-ef-core) in an [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects)?

## Replies

### Reply by Aryan Kumar

Handling database migrations and [schema](https://www.mindstick.com/articles/1844/how-schema-markup-useful-in-serp) changes in an [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) [MVC project](https://www.mindstick.com/forum/158706/how-do-you-establish-a-database-connection-in-an-asp-dot-net-mvc-project) is commonly done using a tool called Entity Framework Migrations. Entity Framework is an Object-Relational Mapping (ORM) framework that simplifies database operations in .NET applications. Migrations allow you to evolve the database schema over time as your application evolves.

Here's a step-by-step guide on how to [handle database](https://www.mindstick.com/forum/158711/how-can-handle-database-transactions-in-asp-dot-net-mvc) migrations in an ASP.NET MVC project using Entity Framework:

### 1. Install Entity Framework:

Ensure that Entity Framework is installed in your project. If it's not, you can install it using the following NuGet Package Manager Console command:

```plaintext
Install-Package Microsoft.EntityFrameworkCore
```

### 2. Create DbContext:

Create a class that inherits from **DbContext** to represent your database context. This class will include a **DbSet** property for each entity you want to persist in the database.

```plaintext
public class ApplicationDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    // Other DbSet properties for your entities...

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("YourConnectionString");
    }
}
```

### 3. Enable Migrations:

In the Package Manager Console, run the following command to enable migrations:

```plaintext
Add-Migration InitialCreate
```

This command generates a migration file in your project.

### 4. Update Database:

Apply the migration to update the database:

```plaintext
Update-Database
```

This command executes the SQL statements in the migration file to create or update the database schema.

### 5. Model Changes:

When you make changes to your data model, you need to create a new migration and update the database.

#### Create Migration:

```plaintext
Add-Migration NameOfYourMigration
```

#### Update Database:

```plaintext
Update-Database
```

### 6. Automatic Migrations:

You can enable automatic migrations to have Entity Framework automatically create migration files based on changes in your data model.

Add the following line to the **OnConfiguring** method in your **DbContext**:

```plaintext
optionsBuilder.UseSqlServer("YourConnectionString", x => x.MigrationsAssembly("YourProjectName"));
```

### 7. Seeding Data:

You can use the **Seed** method in your migration files to seed initial data into the database.

```plaintext
protected override void Seed(ApplicationDbContext context)
{
    context.Users.Add(new User { Name = "John Doe", Email = "john@example.com" });
    // Add more seed data...
    context.SaveChanges();
}
```

### Important Note:

Always make sure to backup your database before applying migrations, especially in a production environment. Also, consider using version control to track changes to your migration files.

This approach provides a systematic way to handle database migrations and schema changes in your ASP.NET MVC project using Entity Framework. It ensures that your database schema evolves along with your application without manual intervention.


---

Original Source: https://www.mindstick.com/forum/158715/how-to-handle-database-migrations-and-schema-changes-in-an-asp-dot-net-mvc-project

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
