Tutorials Entity Framework Core Tutorial
DbSet in EF Core — Complete Guide
DbSet in EF Core — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.
On this page
Entity Framework Core Tutorial · Lesson 6 of 100
DbSet in EF Core
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — Foundations · ~6 min · Module 1: EF Core Fundamentals
What is this?
DbSet
Why should you care?
Every ShopNest feature — search products, list orders, attach customers — starts at _context.Products or _context.Orders.
See it live — copy this example
Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).
// Insert
await _context.Products.AddAsync(new Product { Name = "USB Hub", Price = 899 });
// Query by key
var hub = await _context.Products.FindAsync(42);
await _context.SaveChangesAsync();
What happened?
- AddAsync marks a new Product as Added.
- FindAsync translates to a primary-key SELECT.
- SaveChangesAsync sends INSERT for Added entities.
Practice next
- Insert one product and print its Id after SaveChangesAsync.
- Use FindAsync vs FirstOrDefaultAsync(p => p.Id == id) and compare SQL in logs.
- Remove a product with Remove and observe Deleted state before save.
- Attach an existing Product with _context.Products.Attach and set State to Modified.
- Query _context.Products.Local to see tracked entities not yet saved.
Remember
DbSet is a typed table gateway. Add/Remove stage changes; SaveChangesAsync persists them. FindAsync is optimized for primary-key lookup.
Admin adds SKU through DbSet
ShopNest back-office tool calls _context.Products.AddAsync for a new listing then SaveChangesAsync once.
Outcome: Single INSERT with identity Id returned for the catalog API.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!