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.
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.
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.
appsettings.Environment.json using the JSON configuration provider. For example, appsettings . Production . json and appsettings . Development . json .
App secrets when the app runs in the Development environment.
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