CRUD Operations Using Entity Framework in C#
Table of Contents
- Introduction
- What is Entity Framework?
- Setting Up Entity Framework
- Create Operation
- Read Operation
- Update Operation
- Delete Operation
- Best Practices
- Conclusion
Introduction
Entity Framework (EF) is one of the most popular ORM (Object-Relational Mapping) frameworks used by C# developers to interact with databases efficiently. It simplifies repetitive database tasks such as inserting, reading, updating, and deleting data—commonly known as CRUD operations. This blog walks you through performing CRUD operations using Entity Framework in C#, complete with examples and best practices.
What is Entity Framework?
Entity Framework is Microsoft’s ORM for .NET applications that allows developers to work with relational databases without writing raw SQL queries. It supports:
- Code First Approach
- Database First Approach
- Model First Approach
For this tutorial, we use the Code First approach.
Setting Up Entity Framework
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Then define your model:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Create the DbContext:
public class AppDbContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("Server=.;Database=EFDB;Trusted_Connection=True;");
}
}
Create Operation (Insert)
using (var db = new AppDbContext())
{
var emp = new Employee
{
Name = "Gokul",
Age = 25
};
db.Employees.Add(emp);
db.SaveChanges();
}
Read Operation (Fetch)
// Get all employees
using (var db = new AppDbContext())
{
var employees = db.Employees.ToList();
foreach (var e in employees)
{
Console.WriteLine($"{e.Id} - {e.Name} - {e.Age}");
}
}
Update Operation
using (var db = new AppDbContext())
{
var emp = db.Employees.FirstOrDefault(e => e.Id == 1);
if (emp != null)
{
emp.Name = "Gokul Updated";
emp.Age = 26;
db.SaveChanges();
}
}
Delete Operation
using (var db = new AppDbContext())
{
var emp = db.Employees.FirstOrDefault(e => e.Id == 1);
if (emp != null)
{
db.Employees.Remove(emp);
db.SaveChanges();
}
}
Best Practices
- Use async/await for database operations.
- Always validate input data before saving.
- Keep DbContext lifetime short.
- Use migrations for schema updates.
- Apply repository & unit-of-work patterns for large projects.
Conclusion
CRUD operations are the foundation of any data-driven application, and Entity Framework makes them easier, cleaner, and more efficient. Whether you're building enterprise applications or learning backend development, mastering EF’s CRUD operations will greatly improve your productivity in C# projects.
This Content Sponsored by SBO Digital Marketing. Mobile-Based Part-Time Job Opportunity by SBO! Earn money online by doing simple content publishing and sharing tasks. Here's how: Job Type: Mobile-based part-time work Work Involves: Content publishing Content sharing on social media Time Required: As little as 1 hour a day Earnings: ₹300 or more daily Requirements: Active Facebook and Instagram account Basic knowledge of using mobile and social media For more details: WhatsApp your Name and Qualification to 9025032394 a.Online Part Time Jobs from Home b.Work from Home Jobs Without Investment c.Freelance Jobs Online for Students d.Mobile Based Online Jobs e.Daily Payment Online Jobs Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob


0 Comments