Curriculum
Code First Approach and Migrations in Entity Framework Core are among the most powerful features of EF Core. Instead of creating database tables manually, developers create C# classes, and Entity Framework Core automatically generates and manages the database structure. This approach improves productivity, supports version control, and simplifies database management throughout the software development lifecycle.
Understanding Code First Approach and Migrations in Entity Framework Core is essential because most modern ASP.NET Core applications use this methodology to build scalable, maintainable, and enterprise-grade database applications.
The Code First Approach means:
Write C# Classes
↓
Generate Database
↓
Manage Schema Through Code
Developers design application models first, and EF Core creates the database structure automatically.
Without Code First:
Create Tables Manually
Manage Database Changes Manually
Write SQL Scripts Frequently
With Code First:
Automatic Table Creation
Automatic Schema Updates
Version Controlled Database
This significantly improves development efficiency.
Traditional Process:
Database First
↓
Tables Created
↓
Application Built
Database design starts before application development.
Code First Process:
Models Created
↓
Database Generated
↓
Application Developed
The application drives database creation.
Example:
public class Student
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string Course
{
get;
set;
}
}
This model becomes a database table.
Entity:
Student
Database:
Students Table
Properties become columns automatically.
Example:
Students
----------------
Id
Name
Course
EF Core creates the table based on the entity.
Migrations are a feature that manages database schema changes over time.
Purpose:
Track Database Changes
Apply Updates
Maintain Versions
Migrations keep databases synchronized with application models.
Applications evolve.
Examples:
Add Columns
Remove Columns
Rename Columns
Create Tables
Migrations manage these changes safely.
Modify Model
↓
Create Migration
↓
Apply Migration
↓
Update Database
This is the standard EF Core workflow.
Example:
public class
ApplicationDbContext :
DbContext
{
public DbSet<Student>
Students
{
get;
set;
}
}
DbContext is required before migrations can be used.
Common Package:
Microsoft.EntityFrameworkCore.Tools
These tools enable migration commands.
Program.cs:
builder.Services
.AddDbContext<
ApplicationDbContext>(
options =>
options.UseSqlServer(
connectionString));
EF Core now knows how to connect to the database.
Package Manager Console:
Add-Migration
InitialCreate
Result:
Migration File Created
This migration represents the initial database structure.
Examples:
InitialCreate
AddStudentTable
AddCourseColumn
CreateAttendanceTable
Use descriptive migration names.
EF Core generates:
Migration Class
Snapshot File
These files track schema history.
Generated Code:
migrationBuilder
.CreateTable(
name: "Students");
EF Core automatically creates SQL instructions.
EF Core maintains:
Model Snapshot
Purpose:
Track Current Schema
Compare Future Changes
This helps EF Core generate accurate migrations.
Command:
Update-Database
Result:
Database Created
Tables Created
The migration is applied to the database.
Example:
Students Table Created
The schema now matches the entity model.
Example:
public string Email
{
get;
set;
}
The model changes.
Command:
Add-Migration
AddStudentEmail
EF Core detects the new property.
Generated Action:
Add Column Email
The migration updates the schema.
Command:
Update-Database
Result:
Email Column Added
The database stays synchronized.
EF Core creates:
__EFMigrationsHistory
Purpose:
Track Applied Migrations
This table prevents duplicate migration execution.
Command:
Remove-Migration
Purpose:
Delete Last Migration
Useful when a migration was created incorrectly.
Example:
Update-Database
InitialCreate
The database returns to a previous migration state.
InitialCreate
↓
AddStudentEmail
↓
AddCourseTable
↓
AddAttendanceTable
Each migration represents a database change.
Database structures are generated automatically.
Schema originates from C# classes.
Database changes are tracked in source control.
Database updates become predictable.
All developers share migration history.
These benefits make Code First highly popular.
Student Management System:
Entities:
Student
Teacher
Course
Attendance
Result
Migrations generate all required tables automatically.
Entities:
Doctor
Patient
Appointment
Prescription
Database structure evolves through migrations.
Entities:
Product
Category
Customer
Order
Payment
Code First creates and updates the schema.
Create Migration:
Add-Migration
MigrationName
Apply Migration:
Update-Database
Remove Migration:
Remove-Migration
Generate SQL Script:
Script-Migration
These commands are frequently used.
Reduces manual work.
Tracks changes over time.
Supports continuous development.
Database changes become part of the project.
Works well for large applications.
These advantages improve software development workflows.
May create schema issues.
Database will not be updated.
Makes maintenance difficult.
Can complicate troubleshooting.
May cause deployment problems.
Code First creates database structures from C# classes.
Migrations manage database schema changes over time.
Add-Migration.
Update-Database.
A table that tracks applied migrations.
They simplify database creation, updates, and schema version management.
The Code First Approach creates database tables from C# entity classes.
Migrations are used to track and apply database schema changes.
Add-Migration creates a new migration file.
Update-Database applies migrations to the database.
Code First improves productivity and keeps the database synchronized with application code.
They provide automated database creation, schema management, and version control capabilities.
WhatsApp us