Curriculum
DbContext, DbSet, and Entity Models in Entity Framework Core are the three fundamental building blocks of every EF Core application. Understanding DbContext, DbSet, and Entity Models in Entity Framework Core is essential because these components work together to connect applications with databases, manage data operations, and map database tables to C# classes.
Every ASP.NET Core application that uses Entity Framework Core relies heavily on DbContext, DbSet, and Entity Models in Entity Framework Core. Whether you are developing a School Management System, Hospital Management System, Banking Application, CRM, ERP Platform, E-Commerce Website, or Enterprise Software, these concepts form the foundation of database interaction.
Entity Models represent database tables using C# classes.
Example:
public class Student
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string Course
{
get;
set;
}
}
This class represents a Student table.
Without Entity Models:
Manual Table Handling
Manual Data Mapping
Complex Database Code
With Entity Models:
Strong Typing
Automatic Mapping
Cleaner Code
Entity Models improve maintainability.
Entity:
public class Student
{
}
Database:
Student Table
Properties:
public string Name
{
get;
set;
}
Database:
Name Column
EF Core performs this mapping automatically.
Student Management System:
Student
Teacher
Course
Attendance
Result
Each entity becomes a table.
DbContext is the primary class responsible for interacting with the database.
Purpose:
Database Connection
Data Retrieval
Data Updates
Change Tracking
DbContext acts as a bridge between the application and the database.
Without DbContext:
No Database Communication
DbContext manages:
Queries
Updates
Deletes
Inserts
It is the heart of Entity Framework Core.
Application
↓
DbContext
↓
Database
All database operations pass through DbContext.
Example:
public class
ApplicationDbContext :
DbContext
{
}
This class represents the application’s database context.
Example:
public class
ApplicationDbContext :
DbContext
{
}
Inheritance provides EF Core functionality automatically.
Example:
public
ApplicationDbContext(
DbContextOptions<
ApplicationDbContext>
options)
: base(options)
{
}
The constructor receives database configuration.
DbSet represents a database table inside DbContext.
Example:
public DbSet<Student>
Students
{
get;
set;
}
Meaning:
Student Table
DbSet provides access to table data.
DbSet enables:
Insert Records
Retrieve Records
Update Records
Delete Records
Almost all CRUD operations use DbSet.
public class
ApplicationDbContext :
DbContext
{
public
ApplicationDbContext(
DbContextOptions<
ApplicationDbContext>
options)
: base(options)
{
}
public DbSet<Student>
Students
{
get;
set;
}
public DbSet<Course>
Courses
{
get;
set;
}
}
This context manages multiple tables.
Example:
Students
Courses
Teachers
Results
Each DbSet represents a separate table.
Entity Model
↓
DbSet
↓
DbContext
↓
Database
All components work together.
Database Row:
Student Record
C# Object:
Student student =
new Student();
Rows become objects.
Example:
Student student =
new Student
{
Name = "Rahul",
Course = ".NET"
};
This object can be saved to the database.
Example:
context.Students
.Add(student);
The entity is added to the context.
Example:
context
.SaveChanges();
EF Core writes changes to the database.
Purpose:
Insert Data
Update Data
Delete Data
Changes remain temporary until SaveChanges is called.
Example:
var students =
context.Students
.ToList();
Result:
All Student Records
Data is retrieved from the database.
Example:
var student =
context.Students
.Find(1);
Result:
Student With Id 1
Find searches by primary key.
Example:
student.Name =
"Rahul Sharma";
context
.SaveChanges();
EF Core updates the database automatically.
Example:
context.Students
.Remove(student);
context
.SaveChanges();
The record is removed from the database.
EF Core automatically tracks changes made to entities.
Example:
Original Data
↓
Modified Data
↓
SaveChanges()
Updates are detected automatically.
Automatic Updates
Reduced Coding
Improved Productivity
Developers write less code.
Typically registered as:
Scoped
Meaning:
One Instance
Per Request
This is the recommended approach.
Program.cs:
builder.Services
.AddDbContext<
ApplicationDbContext>(
options =>
{
});
This enables dependency injection.
Controller:
private readonly
ApplicationDbContext
context;
Constructor:
public StudentController(
ApplicationDbContext
context)
{
this.context =
context;
}
The framework injects the context automatically.
Example:
public int Id
{
get;
set;
}
EF Core automatically treats:
Id
as the primary key.
Entity:
Student
DbSet:
Students
Table:
Students
Following conventions simplifies development.
School Management System:
Entities:
Student
Teacher
Course
Attendance
Result
DbSets:
Students
Teachers
Courses
Attendances
Results
DbContext manages all entities.
Entities:
Doctor
Patient
Appointment
Prescription
DbContext manages all healthcare data.
Entities:
Customer
Account
Transaction
Loan
EF Core maps these entities to database tables.
Compile-time validation.
Objects and tables stay synchronized.
Reduces repetitive code.
Simplifies application updates.
Improves developer experience.
These advantages make EF Core highly efficient.
Can impact performance.
Changes will not persist.
Reduce maintainability.
Creates confusion.
Violates clean architecture principles.
DbContext is the primary EF Core class used to interact with databases.
DbSet represents a database table.
An Entity Model is a C# class that represents a database table.
SaveChanges writes changes to the database.
Change Tracking automatically detects modifications to entities.
They form the foundation of Entity Framework Core database operations.
DbContext is the central class responsible for database communication and data management.
DbSet represents a database table and provides CRUD functionality.
An Entity Model is a C# class that maps to a database table.
SaveChanges commits pending changes to the database.
Change Tracking automatically detects entity modifications.
They provide the foundation for database access, object mapping, and data management in ASP.NET Core applications.
WhatsApp us