Roadmap › Entity Framework (EF6 & EF Core) › Module: Saving Data
Lessons in this course (15)

10. Add, update, delete and SaveChanges

Basic★ Must knowLesson 10 of 15
csharp
// Add
db.Customers.Add(new Customer { Name = "Acme" });
await db.SaveChangesAsync(ct);

// Update — tracked (connected) scenario
var p = await db.Products.FindAsync(id);
p!.Price = 499;                                 // change tracker notices
await db.SaveChangesAsync(ct);                  // UPDATE Products SET Price = @p0 WHERE Id = @p1

// Update — disconnected (e.g. DTO from API)
db.Products.Update(product);                     // marks ALL properties modified
db.Entry(product).Property(x => x.Price).IsModified = true;   // or mark only specific ones

// Delete
db.Products.Remove(p);
await db.SaveChangesAsync(ct);

SaveChanges wraps all pending changes in one transaction and batches statements (SQL Server default batch up to 42 statements). Interceptors (SaveChangesInterceptor) or overriding SaveChangesAsync are common for audit fields (CreatedAt, ModifiedBy).