Skip to content

Latest commit

 

History

History
163 lines (113 loc) · 6.28 KB

File metadata and controls

163 lines (113 loc) · 6.28 KB

Lab 4 - Add data access and persistence

.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.)

Adding PostgreSQL database to eShopLite.AppHost

  1. Get the repository root.

    # bash/zsh
    REPOSITORY_ROOT=$(git rev-parse --show-toplevel)
    # PowerShell
    $REPOSITORY_ROOT = git rev-parse --show-toplevel
  2. Navigate to Lab 4.

    cd "$REPOSITORY_ROOT/Labs/Lab 4 - Data"
  3. Add the Aspire.Hosting.PostgreSQL package to the eShopLite.AppHost project.

    dotnet add ./eShopLite.AppHost package Aspire.Hosting.PostgreSQL
  4. Open the AppHost.cs file from the eShopLite.AppHost project.

  5. 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 the products using the WithReference method.

    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 👆👆👆
  6. 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.

Configure Product API to use a PostgreSQL database

  1. Make sure you're still in Lab 4.

    cd "$REPOSITORY_ROOT/Labs/Lab 4 - Data"
  2. Add the Aspire.Hosting.PostgreSQL package to the Products project.

    dotnet add ./Products package Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
  3. Open the appsettings.json file from the Products project and remove the ConnectionStrings section completely. It should look like this with it removed:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*"
    }
  4. Open the Program.cs file 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.

Explore the .NET Aspire dashboard

Let's test the whole application.

  1. Make sure Docker Desktop is up and running

  2. Make sure you're still in Lab 4.

    cd "$REPOSITORY_ROOT/Labs/Lab 4 - Data"
  3. Run the .NET Aspire app.

    dotnet watch run --project ./eShopLite.AppHost
  4. When the .NET Aspire dashboard appears, note the you have many more resources.

    Dashboard with PostgreSQL

    NOTE: You may be asked to enter an authentication token to access to the dashboard.

    .NET Aspire dashboard login

    The token can be found in the terminal console. Copy and paste it to the field and click "Log in".

    .NET Aspire dashboard access token

  5. Click on the pg-pgadmin resource, a new tab will open with the pgAdmin website. It can takes a few seconds to completely load.

  6. 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.

    postgresql admin dashboard

  7. You can see all the rows by right-clicking on the products table and selecting View/Edit Data > All Rows.

    all the data in the products table viewed from the postgresql admin dashboard

  8. Now going back the the .NET Aspire dashboard, click on store the endpoints, a new tab will open with the store website.

  9. The store works like before but now uses a PostgreSQL database in a container.

Deploy / Re-deploy the solution with a PostgreSQL database

  1. If you you are in a folder from where the solution was never deployed, azd init then azd up will deploy the solution without any problem.

    If you are in the same folder where the solution was previously deployed, then just run azd up to deploy the updated application including the PostgreSQL database.

  2. Wait for the deployment to complete. It should be faster than the first time because most of the resources are already created.

  3. Once the deployment is over, click on the link store from the (azd deploy) outputs, or go to the Azure Portal and navigate to the resource group of rg-<RANDOM_NAME> and find the Azure Container Apps named store.

  4. The store should be working as before, but now it uses a PostgreSQL database.

Clean up

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 ->