Curriculum
CRUD Operations using Entity Framework Core are among the most important skills for ASP.NET Core developers. Almost every business application performs CRUD Operations using Entity Framework Core to manage data stored in databases. Whether you are developing a Student Management System, Hospital Management System, Banking Application, CRM, ERP, E-Commerce Platform, Inventory Management System, or SaaS Product, CRUD operations are used daily.
CRUD stands for Create, Read, Update, and Delete. Entity Framework Core simplifies these operations by allowing developers to work with C# objects instead of writing complex SQL statements.
CRUD stands for:
Create
Read
Update
Delete
These operations represent the four basic database actions.
Most applications need to:
Add Data
View Data
Modify Data
Remove Data
CRUD operations make this possible.
User Input
↓
Application
↓
Entity Framework Core
↓
Database
EF Core handles communication with the database.
Example:
public class Student
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string Course
{
get;
set;
}
}
This entity represents a Student table.
Example:
public class
ApplicationDbContext :
DbContext
{
public DbSet<Student>
Students
{
get;
set;
}
}
The DbSet provides access to student records.
Create means:
Insert New Data
Example:
Add New Student
Data is stored in the database.
Example:
Student student =
new Student
{
Name = "Rahul",
Course = ".NET"
};
The object exists in memory.
Example:
context.Students
.Add(student);
The entity is added to the context.
Example:
context
.SaveChanges();
Result:
Record Saved
SaveChanges commits data to the database.
Student student =
new Student
{
Name = "Rahul",
Course = ".NET"
};
context.Students
.Add(student);
context
.SaveChanges();
A new student record is created.
Create Object
↓
Add To DbSet
↓
SaveChanges()
↓
Database Insert
This is the standard insert process.
Read means:
Retrieve Data
Example:
View Student Records
Data is fetched from the database.
Example:
var students =
context.Students
.ToList();
Result:
All Students
ToList executes the query.
Purpose:
Execute Query
Convert Results
Return Collection
Frequently used in EF Core.
Example:
var student =
context.Students
.Find(1);
Result:
Student With Id 1
Find searches by primary key.
Example:
var student =
context.Students
.FirstOrDefault(
s => s.Name ==
"Rahul");
Returns:
Matching Student
Useful for searching records.
Example:
var students =
context.Students
.Where(
s => s.Course ==
".NET")
.ToList();
Result:
.NET Students
Filtering improves query precision.
Database Query
↓
EF Core
↓
Objects
↓
Application
Records become C# objects.
Update means:
Modify Existing Data
Example:
Change Student Name
The existing record is updated.
Example:
var student =
context.Students
.Find(1);
Retrieve the record before modifying it.
Example:
student.Name =
"Rahul Sharma";
The object changes in memory.
Example:
context
.SaveChanges();
EF Core updates the database automatically.
var student =
context.Students
.Find(1);
student.Name =
"Rahul Sharma";
context
.SaveChanges();
The database record is updated.
Retrieve Entity
↓
Modify Properties
↓
SaveChanges()
↓
Database Update
EF Core tracks modifications automatically.
EF Core monitors:
Original Values
Modified Values
During SaveChanges:
Automatic Update Detection
This reduces coding effort.
Delete means:
Remove Data
Example:
Delete Student Record
The record is permanently removed.
Example:
var student =
context.Students
.Find(1);
The entity must be loaded first.
Example:
context.Students
.Remove(student);
The entity is marked for deletion.
Example:
context
.SaveChanges();
Result:
Record Deleted
The deletion becomes permanent.
var student =
context.Students
.Find(1);
context.Students
.Remove(student);
context
.SaveChanges();
The student record is removed.
Find Entity
↓
Remove Entity
↓
SaveChanges()
↓
Database Delete
This is the standard deletion process.
SaveChanges handles:
Insert
Update
Delete
Without SaveChanges:
No Database Changes
This is a common beginner mistake.
Modern applications often use asynchronous operations.
Examples:
await context
.SaveChangesAsync();
await context
.Students
.ToListAsync();
Async operations improve scalability.
Benefits:
Better Performance
Improved Scalability
Efficient Resource Usage
Recommended for web applications.
Workflow:
Controller
↓
DbContext
↓
Database
↓
View
Most MVC applications follow this pattern.
CRUD Operations:
Add Student
View Student
Update Student
Delete Student
These are core application functions.
CRUD Operations:
Add Patient
View Records
Update Details
Delete Appointments
Healthcare applications rely heavily on CRUD.
CRUD Operations:
Add Product
View Product
Update Price
Delete Product
CRUD powers product management systems.
Work with objects instead of queries.
Compile-time validation.
Reduces coding effort.
Faster development.
Supports multiple databases.
These benefits improve developer efficiency.
Changes will not persist.
Can cause exceptions.
Reduces performance.
May affect scalability.
Reduces maintainability.
Create, Read, Update, and Delete.
SaveChanges() or SaveChangesAsync().
DbSet represents a database table and supports CRUD operations.
Find retrieves a record using its primary key.
To improve performance and scalability.
They provide the foundation for managing application data.
CRUD Operations are Create, Read, Update, and Delete actions performed on data.
SaveChanges() and SaveChangesAsync() save data to the database.
DbSet represents a database table and enables CRUD operations.
Change Tracking automatically detects modifications to entities.
It improves performance and scalability in web applications.
They allow applications to create, retrieve, update, and delete database records efficiently.
WhatsApp us