When I first saw EF Core migrations, I knew the commands:
Add-Migration InitialCreate
Update-Database
But knowing the commands and understanding what they actually do are two different things.
For a while, migrations felt like some EF Core magic that changed the database for me.
The basic idea is actually pretty simple.
A migration is a set of instructions that tells EF Core:
“Here is how the database structure needs to change.”
Start With a C# Entity
Suppose I have this entity:
public class Account
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Balance { get; set; }
}
EF Core can use that class as part of the database model.
The flow looks like this:
C# Entity Classes
↓
EF Core
↓
Database Model
But changing a C# class does not automatically rewrite an existing database.
That is where migrations come in.
What Does Add-Migration Do?
Suppose I add a new property:
public string AccountType { get; set; } = string.Empty;
Now my C# model changed.
BEFORE
Account
- Id
- Name
- Balance
AFTER
Account
- Id
- Name
- Balance
- AccountType
But the database still has the old structure.
So I create a migration:
Add-Migration AddAccountType
EF Core compares the current model with its previous model snapshot.
Then it creates migration code describing the difference.
A simplified migration might contain:
migrationBuilder.AddColumn<string>(
name: "AccountType",
table: "Accounts",
type: "nvarchar(max)",
nullable: false,
defaultValue: "");
The important part:
Add-Migration does not normally update the database itself.
It creates the instructions.
I remember it like this:
Add-Migration
=
Write down what needs to change
What Does Update-Database Do?
Now I run:
Update-Database
This tells EF Core:
“Take the migrations that have not been applied yet and apply them to the database.”
So:
C# model changed
↓
Add-Migration
↓
Migration file created
↓
Update-Database
↓
Database changed
That distinction is extremely useful.
Add-Migration
CREATES the database change instructions
Update-Database
APPLIES those instructions
What Is Inside a Migration?
A migration usually contains two main methods:
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
I think of them like this:
Up()
Move database FORWARD
Down()
Move database BACKWARD
For example:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "AccountType",
table: "Accounts",
nullable: false,
defaultValue: "");
}
And the reverse might be:
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AccountType",
table: "Accounts");
}
The Up() method says:
Add the column.
The Down() method says:
Remove it again if this migration is rolled back.
What Is the Model Snapshot?
EF Core also keeps a file usually ending with:
ModelSnapshot.cs
This represents EF Core’s current understanding of your model.
A simplified way to think about it:
Current C# Model
↓ compare
Previous Model Snapshot
↓
What changed?
↓
New Migration
EF Core needs some way to know whether you added, removed, or changed something.
The snapshot helps it do that.
Example: Adding a Transaction Entity
Imagine I add this:
public class Transaction
{
public int Id { get; set; }
public DateTime Date { get; set; }
public decimal Amount { get; set; }
public string? Notes { get; set; }
}
Then I add it to my DbContext:
public DbSet<Transaction> Transactions { get; set; }
Now I can create a migration:
Add-Migration AddTransactions
Then:
Update-Database
Conceptually:
Transaction.cs
↓
DbContext
↓
Add-Migration
↓
CREATE TABLE instructions
↓
Update-Database
↓
Transactions table
What If I Make a Mistake?
Suppose I create a migration but realize it is wrong before applying it.
I can usually remove the last migration:
Remove-Migration
Then fix the model and create it again.
But once a migration has been applied to databases other people use, I would be much more careful.
Instead of casually deleting migration history, it is often better to create another migration that corrects the problem.
That matters on a real development team because migrations become part of the application’s history.
Why Keep Migration Files in Git?
Migration files are code.
So they normally belong in source control.
That means another developer can pull the project and see:
InitialCreate
AddAccounts
AddTransactions
AddCategories
AddTransactionIndex
...
It becomes a history of how the database schema changed.
Migrations Are Not the Data
This was another useful distinction for me.
A migration usually changes the structure of a database.
For example:
Add table
Add column
Remove column
Create foreign key
Create index
It is not the same thing as the normal data inside the tables.
Think:
DATABASE SCHEMA
How the database is built
DATABASE DATA
What is stored inside it
What I Would Say in an Interview
If someone asked:
“What are EF Core migrations?”
I would say:
EF Core migrations are versioned code files that describe changes to the database schema as the application’s entity model changes.
Add-Migrationgenerates a migration based on model differences, whileUpdate-Databaseapplies pending migrations to the database.
My super-short memory trick is:
Change C# model
↓
Add-Migration = create instructions
↓
Update-Database = run instructions
Once I understood those as two separate jobs, migrations stopped feeling mysterious.
