---
title: "How do you perform migrations in EF Core?"  
description: "How do you perform migrations in EF Core?"  
author: "Utpal Vishwas"  
published: 2025-05-20  
updated: 2025-05-20  
canonical: https://www.mindstick.com/interview/34146/how-do-you-perform-migrations-in-ef-core  
category: "c#"  
tags: ["c#", "api(s)", ".net core"]  
reading_time: 4 minutes  

---

# How do you perform migrations in EF Core?

To perform **migrations in EF Core**, you use the **EF Core CLI tools** or **Package Manager Console** to:

1. Create migration files (track model changes).
2. Apply those changes to the database.

## Basic EF Core Migration Workflow

1. **Install EF Core Tools (if needed)**

For CLI:

```plaintext
dotnet tool install --global dotnet-ef
```

2. **Create Initial Migration**

```plaintext
dotnet ef migrations add InitialCreate
```

This creates a `Migrations/` folder with a snapshot of the model and SQL instructions to create tables.

3. **Apply Migration to Database**

```plaintext
dotnet ef database update
```

This runs the migration and updates your database schema accordingly.

## When Models Change

**1. Modify your C# models or** `DbContext`**.**

## 2. Add a new migration:

```plaintext
dotnet ef migrations add AddPriceToProduct
```

## 3. Apply it:

```plaintext
dotnet ef database update
```

## Full Example

## Step 1: Define a model

```cs
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}
```

## Step 2: Define a DbContext

```cs
public class AppDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options) { }
}
```

## Step 3: Register DbContext

```cs
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
```

## Step 4: Add & Apply Migration

```plaintext
dotnet ef migrations add InitialCreate
dotnet ef database update
```

## Common Commands

| Command | Purpose |
| --- | --- |
| `dotnet ef migrations add <Name>` | Create a new migration |
| `dotnet ef database update` | Apply migrations to DB |
| `dotnet ef migrations remove` | Remove last (unapplied) migration |
| `dotnet ef migrations list` | List all migrations |
| `dotnet ef database drop` | Drop the database |

## Notes

1. Ensure your project includes a `Microsoft.EntityFrameworkCore.Design` package.
2. Always review migration code before applying, especially in production.

## Answers

### Answer by Utpal Vishwas

To perform **migrations in EF Core**, you use the **EF Core CLI tools** or **Package Manager Console** to:

1. Create migration files (track model changes).
2. Apply those changes to the database.

## Basic EF Core Migration Workflow

1. **Install EF Core Tools (if needed)**

For CLI:

```plaintext
dotnet tool install --global dotnet-ef
```

2. **Create Initial Migration**

```plaintext
dotnet ef migrations add InitialCreate
```

This creates a `Migrations/` folder with a snapshot of the model and SQL instructions to create tables.

3. **Apply Migration to Database**

```plaintext
dotnet ef database update
```

This runs the migration and updates your database schema accordingly.

## When Models Change

**1. Modify your C# models or** `DbContext`**.**

## 2. Add a new migration:

```plaintext
dotnet ef migrations add AddPriceToProduct
```

## 3. Apply it:

```plaintext
dotnet ef database update
```

## Full Example

## Step 1: Define a model

```cs
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}
```

## Step 2: Define a DbContext

```cs
public class AppDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options) { }
}
```

## Step 3: Register DbContext

```cs
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
```

## Step 4: Add & Apply Migration

```plaintext
dotnet ef migrations add InitialCreate
dotnet ef database update
```

## Common Commands

| Command | Purpose |
| --- | --- |
| `dotnet ef migrations add <Name>` | Create a new migration |
| `dotnet ef database update` | Apply migrations to DB |
| `dotnet ef migrations remove` | Remove last (unapplied) migration |
| `dotnet ef migrations list` | List all migrations |
| `dotnet ef database drop` | Drop the database |

## Notes

1. Ensure your project includes a `Microsoft.EntityFrameworkCore.Design` package.
2. Always review migration code before applying, especially in production.


---

Original Source: https://www.mindstick.com/interview/34146/how-do-you-perform-migrations-in-ef-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
