Let's look at a .NET Core application pointing to a database named BlogApp, with a appsettings.json file as shown below:
// appsettings.json file
{
"ConnectionStrings": {
"BlogAppContext": "Server=(localdb)\\mssqllocaldb;Database=BlogApp;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
I wanted this app to point to another database named BlogApp2. So I updated the connection string in the appsettings.json file.
// appsettings.json file (updated to point to BlogApp2)
{
"ConnectionStrings": {
"BlogAppContext": "Server=(localdb)\\mssqllocaldb;Database=BlogApp2;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Now, when I run the application, it still points to the older database BlogApp.
Turns out, in order to point to BlogApp2, I need to change another file called secrets.json which overrides all other appsettings configuration. I had to update this file with the desired database (BlogApp2) information in order to make it work as expected.
Here are the steps to update the secrets.json file.
Step 1
Right click on the project and click Manage User Secrets to open the secrets.json file.
Step 2
Update the secrets.json file with the desired database (BlogApp2 in my case) in the connection string.
// secrets.json file
{
"ConnectionStrings:BlogAppContext": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=BlogApp2;Integrated Security=True"
}
Now when I run the application, I am able to connect to a different database.