Data Access in Blazor

Data Access is the sixth chapter of the free Blazor tutorial. It shows how components get the services and data they need.

What each lesson covers:

  • Dependency Injection: Dependencies and injection, the DI container, registering in Program.cs, built-in dependencies, and @inject, [Inject] and constructor injection with NavigationManager.
  • Services: Why services help, separating logic from UI, a user service with CRUD methods, service interfaces, registering and using services, object lifetimes and sharing logic.
  • Database Handling: EF Core and ORM basics, database setup with connection strings and migrations, entity models, DbContext and DbSet, a CRUD data service with SaveChanges, and database records in components.
  • API Integration: Building an ASP.NET Core Web API with models, controllers and routes, GET, POST, PUT and DELETE endpoints, calling it from Blazor with HttpClient and an API service, and handling loading states and errors.

Every code box compiles your components with the real Razor compiler and runs them on .NET 10 in your browser.

Lessons in this section

  1. Dependency Injection

    Dependency injection in Blazor: dependencies and injection, what the DI container is and why it is used, registering in Program.cs, built-in dependencies, and @inject, [Inject] and constructor injection with NavigationManager.

    • Explain dependencies, injection and the DI container
    • Register a class in Program.cs and ask for its interface
    • Inject with @inject, [Inject] and constructors
    • Use the built-in NavigationManager service
  2. Services

    Services in Blazor: what a service is and why it helps, separating logic from UI, a user service with CRUD methods, service interfaces, registering and injecting services, Transient, Scoped and Singleton lifetimes, and sharing logic across components.

    • Move logic out of components into a service
    • Build a user service with CRUD methods and an interface
    • Register a service with DI and use it in components
    • Pick the right lifetime: Transient, Scoped or Singleton
  3. Database Handling

    Databases in Blazor with EF Core: ORM basics, SQLite setup, connection strings and migrations, entity models, DbContext and DbSet, AddDbContextFactory, a data access service with CRUD and SaveChanges, and showing and changing records from components.

    • Explain what an ORM is and what EF Core does
    • Set up EF Core with SQLite, a connection string and migrations
    • Write an entity model, a DbContext and register it
    • Build a CRUD data service and use it from components
  4. API Integration

    Build and use a Web API: an ASP.NET Core Web API with a Tea model, TeasController and routes, GET, POST, PUT and DELETE endpoints, request and response data, HttpClient and an API service in Blazor, CRUD from components, loading states and friendly error messages.

    • Create a Web API with a model, a controller and routes
    • Write GET, POST, PUT and DELETE endpoints with status codes
    • Call the API from Blazor with HttpClient and an API service
    • Show loading states and friendly error messages