Load ConnectionString from appSettings.Test.json

Hi,

Im trying to set the connectionstring in appSettings.Test.json to be able to use different connectionstrings for different environments. But it seems like in startup of the site it still uses the connectionstring from appsettings.json. I have set the ASPNETCORE_ENVIRONMENT to “Test”.

On a server where the site can connect to the connection string set in appsettings.json it seems to work correctly. there it uses the right connection. But in another server where the connection from appsettings.json isn’t accessible I get error when starting the site.

It seems like the site first connects to the connectionstring from appsettings.json in startup but then later uses the correct one from appSettings.Test.json.

Is this a bug or am I missing something else?

Litium version: 8.1

Sounds that you have something with your environment that not is working as it should. Have you verified the variables etc with the description on Use multiple environments in ASP.NET Core | Microsoft Docs?

If you see how the configuration is loaded (from: Configuration in ASP.NET Core | Microsoft Docs) you can see that the also appsettings.json will be loaded and used and then overriding properties from the other files.

  1. ChainedConfigurationProvider : Adds an existing IConfiguration as a source. In the default configuration case, adds the host configuration and setting it as the first source for the app configuration.
  2. appsettings.json using the JSON configuration provider.
  3. appsettings. Environment .json using the JSON configuration provider. For example, appsettings . Production . json and appsettings . Development . json .
  4. App secrets when the app runs in the Development environment.
  5. Environment variables using the Environment Variables configuration provider.
  6. Command-line arguments using the Command-line configuration provider.

Check that your appsettings.test.json contains correct elements that you want to override.

1 Like

Found the problem. We used the connectionstring for a DBContext, but loaded the config for that wrong directly from appSettings.json.
By using connectionstring from IOptions instead it works correctly

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.