CRUD Operations with EF Core
Perform Create, Read, Update, and Delete operations using EF Core's change tracking and SaveChanges.
CRUD Overview in EF Core
EF Core handles all four CRUD operations — Create, Read, Update, Delete — through a combination of change tracking and SaveChangesAsync(). Understanding the change tracker is the key to mastering CRUD.
Create: Adding a New Entity
Use DbSet.Add() or AddAsync() to mark an entity as Added. On SaveChangesAsync(), EF Core generates an INSERT and populates the auto-increment key.
var order = new Order
{
CustomerId = 5,
PlacedAt = DateTime.UtcNow,
Status = OrderStatus.Pending
};
await _db.Orders.AddAsync(order);
await _db.SaveChangesAsync();
Console.WriteLine(order.Id); // populated by DBAll lessons in this course
- DbContext & DbSet Basics
- Migrations & Schema Management
- CRUD Operations with EF Core
- Relationships: One-to-Many & Many-to-Many