Entity Framework Core Interview Question #4
What is DbSet<TEntity>?
EF Core Fundamentals & DbContext Junior Beginner
Quick Interview Answer
DbSet<TEntity> represents a collection of entities of a particular type that EF Core can query and persist. It is commonly exposed as a property on DbContext.
Detailed Explanation
DbSet<TEntity> represents a collection of entities of a particular type that EF Core can query and persist. It is commonly exposed as a property on DbContext.
LINQ operations on a DbSet build database queries. Add, Update, Remove, Find, and related operations interact with EF Core's change tracker and persistence workflow.
A DbSet roughly corresponds to an entity type rather than always mapping one-to-one with a physical table in every advanced mapping scenario.
Code Example
public DbSet<Product> Products { get; set; }
var product = await db.Products
.FirstOrDefaultAsync(p => p.Id == id);