EF Core CRUD Operations
Learn how to Create, Read, Update, and Delete data with Entity Framework Core.
Create
var task = new TaskItem { Title = "Learn EF Core" };context.Tasks.Add(task);await context.SaveChangesAsync();Read
// Get all tasksList<TaskItem> allTasks = await context.Tasks.ToListAsync();
// Get one by primary keyTaskItem? task = await context.Tasks.FindAsync(1);
// Query with LINQList<TaskItem> incomplete = await context.Tasks .Where(t => !t.IsComplete) .ToListAsync();Update
TaskItem? task = await context.Tasks.FindAsync(1);if (task != null){ task.IsComplete = true; await context.SaveChangesAsync(); // EF Core tracks the change automatically}Delete
TaskItem? task = await context.Tasks.FindAsync(1);if (task != null){ context.Tasks.Remove(task); await context.SaveChangesAsync();}SaveChangesAsync Does the Real Work
Add/Remove and property changes only update EF Core's in-memory tracking. Nothing actually hits the database until you call SaveChangesAsync().
FAQs
EF Core automatically tracks entities it has loaded — modifying a tracked object's properties is enough; SaveChangesAsync() detects the change.
Summary
- Add() stages a new entity for insertion.
- LINQ queries against DbSet<T> read data, translated to SQL.
- Modifying a tracked entity and calling SaveChangesAsync() handles both updates and deletes.