What is DbSet and how is it used?

Beginner

Answer

DbSet<T> represents a collection of entities of a specific type that can be queried from the database. It provides an abstraction over database tables.
Key features:

  • Querying: Supports LINQ queries
  • CRUD operations: Add, Remove, Update operations
  • Change tracking: Tracks entity states
  • Local view: Access to locally tracked entities
// Querying
var products = context.Products.Where(p => p.Price > 100).ToList();
// Adding
context.Products.Add(new Product { Name = "New Product" });
// Removing
context.Products.Remove(product);
// Local entities
var localProducts = context.Products.Local.ToList();