Entity Framework Core (EF Core) is a powerful Object-Relational Mapper (ORM) that enables .NET developers to work with databases using C# classes rather than writing SQL queries manually. Among the ways to work with EF Core, the Code First approach is one of the most widely used in modern application development.
In the Code First approach, the database is created and managed entirely from C# code. Developers first design domain models (classes), and Entity Framework Core automatically generates the database schema from those classes. Any future changes to the models are applied to the database using migrations, ensuring the application and database stay in sync.
This article explains the Code First approach in EF Core in a fully descriptive manner, covering concepts, workflow, components, and real-world usage.

What Is the Code First Approach in EF Core?
The Code First approach means:
You start with C# classes (entities)
EF Core generates tables, columns, relationships, and constraints
The database structure is derived directly from your code
Changes in code are reflected in the database using migrations
In this approach, code is the source of truth, not the database.
Why Use the Code First Approach?
The Code First approach is preferred in many projects because:
Strong Domain-Driven Design: Developers focus on business models instead of database structure.
Better Maintainability: Changes in requirements are handled easily by updating classes.
Version Control Friendly: Database changes are tracked as migration files in source control.
Faster Development: No need to manually create or modify database tables.
Consistency Across Environments: The same schema is applied in development, testing, and production.
How the Code First Workflow Works
The Code First workflow follows a clear sequence:
Create entity classes
Configure the DbContext
Add EF Core packages
Configure database connection
Create migrations
Apply migrations to the database
Update models and re-apply migrations when needed
EF Core takes care of generating SQL behind the scenes.
Step 1: Creating Entity Classes
Entity classes represent tables in the database.
Each property represents a column.
Example: Student Entity
public class Student
{
public int Id { get; set; }
public string FullName { get; set; }
public int Age { get; set; }
public DateTime CreatedDate { get; set; }
}How EF Core Interprets This
Student→ Table nameId→ Primary KeyFullName,Age,CreatedDate→ ColumnsData types are inferred automatically
EF Core follows convention-based mapping unless configured otherwise.
Step 2: Creating the DbContext Class
The DbContext is the central class in EF Core.
It manages database connections, change tracking, and queries.
Example: ApplicationDbContext
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
}Role of DbContext
Acts as a bridge between the application and the database
Keeps track of entity changes
Executes SQL commands
Applies migrations
Manages transactions
Each DbSet<T> represents a database table.
Step 3: Installing Required EF Core Packages
To use Code First, EF Core packages are required.
Common packages:
Microsoft.EntityFrameworkCoreMicrosoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.Tools
These packages enable:
Database provider support
Migrations
Schema generation
Command-line tooling
Step 4: Configuring Database Connection
The database connection is usually stored in configuration files.

Join the conversation! Your thoughts help the community grow.