0Pricing
C# Academy · Lesson

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 DB

All lessons in this course

  1. DbContext & DbSet Basics
  2. Migrations & Schema Management
  3. CRUD Operations with EF Core
  4. Relationships: One-to-Many & Many-to-Many
← Back to C# Academy