Skip to content
Learn Netverks

Lesson

Step 25/36 69% through track

entities-dbcontext

Entities and DbContext

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Entities and DbContext: the syntax, APIs, and habits you need before advancing in ASP.NET.

Teams ship Entities and DbContext on every ASP.NET codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Entities and DbContext in contexts like: Line-of-business APIs, intranets, BFF layers, and cloud-hosted services on Linux or Windows.

Write C# (top-level or Program class), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

An entity is a class representing a table row. DbContext is the session—DbSet<T> properties expose tables, and SaveChangesAsync commits unit-of-work transactions.

Entity example

public class Product {
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public decimal Price { get; set; }
}

DbContext

public class AppDbContext : DbContext {
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) {}
    public DbSet<Product> Products => Set<Product>();
}

Basic usage

await _db.Products.AddAsync(product);
await _db.SaveChangesAsync();

Important interview questions and answers

  1. Q: DbSet vs DbContext?
    A: DbSet is a typed table gateway; DbContext coordinates sets, change tracking, and SaveChanges.
  2. Q: Why inject DbContextOptions?
    A: Options carry connection string and provider config—supports testing with InMemory provider.

Self-check

  1. What method persists changes to the database?
  2. How do you expose a Products table on the context?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Navigation property?
  • Fluent API when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump