Blazor · Data

Entity Framework Core

EF Core works great in Blazor — as long as you respect how long components and circuits live.

Advanced 5 min Lesson 22 of 31Examples target .NET 9 Blazor Web App.
0/31 in Blazor
On this page · 5 min · Advanced

Learning objectives#

  • Register EF Core with a context factory
  • Query and save with short-lived contexts
  • Use AsNoTracking for read-only lists
  • Apply migrations

Explanation#

Register with AddDbContextFactory<ShopDb>. Inject IDbContextFactory<ShopDb> and create a context per operation with await using.

Read-only lists should use AsNoTracking() — faster, and no stale tracked entities.

Never run two queries on the same DbContext at the same time. With a factory, each operation has its own.

Migrations create and evolve the schema: dotnet ef migrations add, dotnet ef database update.

Code example: DbContext, factory and migrations#

C#Data/ShopDb.cs + Program.cs Illustrative
public class ShopDb(DbContextOptions<ShopDb> options) : DbContext(options)
{
    public DbSet<Product> Products => Set<Product>();
    public DbSet<Category> Categories => Set<Category>();
}


// Program.cs
builder.Services.AddDbContextFactory<ShopDb>(o =>
    o.UseSqlServer(builder.Configuration.GetConnectionString("Shop")));


// Usage in a service
await using var db = await factory.CreateDbContextAsync();
var cheap = await db.Products.AsNoTracking()
    .Where(p => p.Price < 5000).OrderBy(p => p.Price).ToListAsync();


// Terminal
// dotnet ef migrations add InitialCreate
// dotnet ef database update
Expected output
The database is created from the model, and the query returns products under ₹5,000 using a fresh, untracked context.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. The DbContext exposes Products and Categories.
  2. AddDbContextFactory makes contexts on demand.
  3. AsNoTracking skips change tracking for read-only data.
  4. Migrations turn the model into tables.

Real-world use case#

The Blazor CRUD project stores products and categories in SQL Server through EF Core.

Common mistakes#

“A second operation was started on this context instance”

Two queries shared one long-lived context. Create a context per operation.

Loading entire tables

Filter, page and project to DTOs in the query.

Best practices#

  • IDbContextFactory in Blazor
  • AsNoTracking for reads
  • Project to DTOs with Select
  • Keep migrations in source control

Remember this#

Blazor + EF Core = factory + await using.

Reads: AsNoTracking.

migrations add → database update.

Practice exercise#

Your task: Update a product’s price with a fresh context.

Show solution
C# Illustrative
await using var db = await factory.CreateDbContextAsync();
var p = await db.Products.FindAsync(id);
if (p is not null) { p.Price = newPrice; await db.SaveChangesAsync(); }

Mini quiz#

Entity Framework Core quizMultiple choice Question 1 of 1

Why use IDbContextFactory in Interactive Server components?

Score so far: 0/0

Want more? Practise all Blazor MCQs · Blazor interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.