.NET Aspire makes it very easy to add a database to your applications. Many SQL-compliant database are already available as .NET Aspire components. In this lab, you will add postgreSQL (or mysql). (This will be optional during the in-person portion of the workshop at Build.)
-
Get the repository root.
# bash/zsh REPOSITORY_ROOT=$(git rev-parse --show-toplevel)
# PowerShell $REPOSITORY_ROOT = git rev-parse --show-toplevel
-
Navigate to Lab 4.
cd "$REPOSITORY_ROOT/Labs/Lab 4 - Data"
-
Add the Aspire.Hosting.PostgreSQL package to the eShopLite.AppHost project.
dotnet add ./eShopLite.AppHost package Aspire.Hosting.PostgreSQL
-
Open the
AppHost.csfile from the eShopLite.AppHost project. -
Let's create a database and passes it to the product API by updating the code. Here save the resource in the variable
productsdb, and pass it to theproductsusing theWithReferencemethod.var redis = builder.AddRedis("redis"); // 👇👇👇 Add 👇👇👇 var productsdb = builder.AddPostgres("pg") .AddDatabase("productsdb"); // 👆👆👆 Add 👆👆👆 var products = builder.AddProject<Projects.Products>("products") // 👇👇👇 Add 👇👇👇 .WithReference(productsdb) .WaitFor(productsdb); // 👆👆👆 Add 👆👆👆
-
Some of the .NET Aspire database components also allow you to create a container for database management tools. To add PgAdmin to your solution to manage the PostgreSQL database, use this code:
var productsdb = builder.AddPostgres("pg") // 👇👇👇 Add 👇👇👇 .WithPgAdmin() // 👆👆👆 Add 👆👆👆 .AddDatabase("productsdb");
The advantage of letting .NET Aspire create the container is that you don't need to do any configuration to connect PgAdmin to the PostgreSQL database, it's all automatic.
-
Make sure you're still in Lab 4.
cd "$REPOSITORY_ROOT/Labs/Lab 4 - Data"
-
Add the Aspire.Hosting.PostgreSQL package to the Products project.
dotnet add ./Products package Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
-
Open the
appsettings.jsonfile from the Products project and remove theConnectionStringssection completely. It should look like this with it removed:{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" } -
Open the
Program.csfile from the Products project and replace the following lines of codes.// Before builder.Services.AddDbContext<ProductDataContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("ProductsContext") ?? throw new InvalidOperationException("Connection string 'ProductsContext' not found."))); // After builder.AddNpgsqlDbContext<ProductDataContext>("productsdb");
It completely replaces the existing SQLite database with PostgreSQL database declared in the eShopLite.AppHost project using the same string
productsdb.
Let's test the whole application.
-
Make sure Docker Desktop is up and running
-
Make sure you're still in Lab 4.
cd "$REPOSITORY_ROOT/Labs/Lab 4 - Data"
-
Run the .NET Aspire app.
dotnet watch run --project ./eShopLite.AppHost
-
When the .NET Aspire dashboard appears, note the you have many more resources.
NOTE: You may be asked to enter an authentication token to access to the dashboard.
The token can be found in the terminal console. Copy and paste it to the field and click "Log in".
-
Click on the pg-pgadmin resource, a new tab will open with the pgAdmin website. It can takes a few seconds to completely load.
-
From the pgAdmin website, you manage the products database. To visualize the products table, expand the Aspire instances node, then pg > Databases > productsdb > Schemas > Tables.
-
You can see all the rows by right-clicking on the products table and selecting View/Edit Data > All Rows.
-
Now going back the the .NET Aspire dashboard, click on store the endpoints, a new tab will open with the store website.
-
The store works like before but now uses a PostgreSQL database in a container.
-
If you you are in a folder from where the solution was never deployed,
azd initthenazd upwill deploy the solution without any problem.If you are in the same folder where the solution was previously deployed, then just run
azd upto deploy the updated application including the PostgreSQL database. -
Wait for the deployment to complete. It should be faster than the first time because most of the resources are already created.
-
Once the deployment is over, click on the link
storefrom the(azd deploy)outputs, or go to the Azure Portal and navigate to the resource group ofrg-<RANDOM_NAME>and find the Azure Container Apps named store. -
The store should be working as before, but now it uses a PostgreSQL database.
To delete all the resources created by the deployment, in Lab 3, you can execute the following command:
azd down --force --purge<- Lab 3 - Deploy to Azure Container Apps | Final Solution ->




