Search⌘ K
AI Features

Getting Entities into a Component

Explore how to integrate the Northwind database context into Blazor components to fetch and display customer data dynamically. Learn to register services, inject dependencies, and render filtered data in web interfaces for interactive user experiences.

Fetch data from the database

Now that we have seen the minimum implementation of a component, we can add some useful functionality to it. In this case, we will use the Northwind database context to fetch customers from the database:

Step 1: In Northwind.BlazorServer.csproj, add a reference to the Northwind database context project for either SQL Server or SQLite, as shown in the following markup:

C#
<ItemGroup>
<!-- change Sqlite to SqlServer if you prefer -->
<ProjectReference Include="..\Northwind.Common.DataContext.Sqlite\Northwind.Common.DataContext.Sqlite.csproj" />
</ItemGroup>

Step 2: Build the Northwind.BlazorServer project.

Step 3: In Program.cs, import the namespace for working with the Northwind database context extension method, as shown in the following code:

C#
using Packt.Shared; // AddNorthwindContext extension method

Step 4: In ...