Entity Framework Core
EF Core works great in Blazor — as long as you respect how long components and circuits live.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 29, 2026
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#
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
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#
- The DbContext exposes Products and Categories.
- AddDbContextFactory makes contexts on demand.
- AsNoTracking skips change tracking for read-only data.
- 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#
Two queries shared one long-lived context. Create a context per operation.
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
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#
Why use IDbContextFactory in Interactive Server components?
Want more? Practise all Blazor MCQs · Blazor interview questions
My notes#
Saved in this browser.