How do I control which database to use when a Blazor Server app starts?

Falanga, Rod, DOH 290 Reputation points
2025-07-03T16:56:57.9066667+00:00

I'm working on a rewrite of an old WebForms application. I want to be able to control, through a GitHub Action running in a self-hosted runner, which database (test or production) the application should run against. I thought I could do this with the OnInitializedAsync() method, but I realized that I'm overriding it:

protected override async Task OnInitializedAsync()
{
    try
    {
        // Call the Jobs API endpoint
        var response = await Http.GetFromJsonAsync<APIResponse>("api/getjobs/true/true");   // getjobs/{prodConn}/{Active}

        if (response != null && response.IsSuccess && response.Result is JsonElement jsonElement)
        {

	// The rest left off for brevity

I would think I couldn't pass in a parameter to OnInitializedAsync(), without dropping override. I looked elsewhere, and came across Blazor - Lifecycle Methods. The OnParametersSet and OnParametersSetAsync methods looked very promising. However, I then read that those parameters come from the querystring. I don't want the user to be able to change which database they run against by changing the querystring. So, how do I change what database is accessed, when a GitHub Action builds and deploys the app?

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-07-03T17:56:46.6933333+00:00
    0 comments No comments

  2. Raymond Huynh (WICLOUD CORPORATION) 620 Reputation points Microsoft External Staff
    2025-07-04T08:14:57.3166667+00:00

    Here’s how you can do it:

    1. Set Environment Variables in GitHub Actions:

    In your GitHub Actions workflow, set the ASPNETCORE_ENVIRONMENT variable to specify which environment the app should use (e.g., Production or Staging).

    2. Use Environment-Specific appsettings Files:

    In your project, create different appsettings files for each environment, each with its own connection string:

    • appsettings.Production.json (for production DB)
    • appsettings.Staging.json (for test DB)

    3. Update appsettings in CI/CD:

    If you need to dynamically update the connection string or other settings during your GitHub Action, you can add a step to modify the appropriate appsettings file before deploying.

    Here’s a helpful guide:

    [How to update appsettings.json files in GitHub Actions] (https://github.com/IntentArchitect/Docs/blob/master/articles/module-building/templates-csharp/how-to-update-appsettings-json-files/how-to-update-appsettings-json-files.md)

    This approach keeps your database selection secure and controlled by your deployment process, not by user input or code changes.

    Hope this helps!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.