TechSha
TechSha

Reputation: 154

.net core not connecting to the changed database in appsettings.json connectionstrings in visual studio debug

I am working on a .net core project, I wanted to change the connectionstring of the database in appsettings.json. I had created a duplicate database and named it originalname_fake01 and made a clone of the original database for testing.

I have also changed the database name in appsettings.developement.json. Everything seems fine but when I run the application in debug mode in visual studio. the data was being pulled from the original database rather than the changed database name in appsettings.json.

Here is my appsettings connectionstrings code: Old connectionstring was

"connectionStrings": {
"MyConnectionString": 
"Server=localhost;port=3306;database=mydb;user=root;password=rt123;"
}

changed connection string (new)

"connectionStrings": {
"FakeConnectionString": 
"Server=localhost;port=3306;database=mydb_fake01;user=root;password=rt123;"
}

I am not able to understand why it is connecting to the old database rather than the new database even after changing the connectionstring.

Any help would be appreciated.

Upvotes: 4

Views: 4129

Answers (5)

Jon
Jon

Reputation: 1

The file named "secrets.json" worked for me as the default connection string was hardcoded there pointing to the old database. Editing the connection string (Initial Catalog=dbName) to point to the current database name fixed the problem. Thanks!

sample content of "secrets.jason": { "ConnectionStrings:DefaultConnection": "Data Source=TestSource;Initial Catalog=dbName;Integrated Security=True;MultipleActiveResultSets=True" }

Upvotes: 0

TechSha
TechSha

Reputation: 154

I have finally able to find the problem in the dbcontext.cs modelbuilder. There is a code line which has strongly typed Schema Name.

protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
         base.OnModelCreating(modelBuilder);
         OnModelCreatingImpl(modelBuilder);

         modelBuilder.HasDefaultSchema("MyDB");
}

I changed it use the new schema as below:

modelBuilder.HasDefaultSchema("MyDB_Fake01");

I don't understand why we need to give schema name in both connectionstring and in modelbuilder. I Guess we should avoid explicit schema name targeting in ModelBuilder so that, whenever the connectionstring is changed. it will target the database correctly.

Anyways, the problem is solved as the application is connecting to the intended database.

Upvotes: 1

Vishal
Vishal

Reputation: 175

There is also a file named "secrets.json" which overrides all other appSettings configuration. Make sure it has correct Database name.

Upvotes: 1

fingers10
fingers10

Reputation: 7947

You are almost there. In your changed connection string (new) setting, change the FakeConnectionString to MyConnectionString. If you have same keys in appsettings.json and in appsettings.development.json then latter will override the former.

"connectionStrings": {
    "MyConnectionString": "Server=localhost;port=3306;database=mydb_fake01;user=root;password=rt123;"
}

P.S. This is applicable to any environment not only to development

Upvotes: 0

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131473

If you want the development settings to override the production settings you need to use the same names and full path. Your connection string should be named MyConnectionString, not MyConnectionString if you want the DbContext to pick it automatically.

JSON setting files have no special meaning in .NET Core, they are just files. Every provider produces key/value pairs in the form Section1:Subsection1:Attribute1, Value. Newer provider values override earlier values. Providers can be JSON or XML file readers, INI file readers, databases etc. In all cases, the settings are flattened to path/value pairs.

The file

"connectionStrings": {
    "MyConnectionString": "Server=localhost;port=3306;database=mydb;user=root;password=rt123;"
}

Produces a value named connectionStrings:MyConnectionString whose value is Server=localhost;port=3306;database=mydb;user=root;password=rt123;. To override this you need to specify a setting with the same path.

The default Host builder specifies some default settings providers. From the docs, those are :

  • appsettings.json.
  • appsettings.{Environment}.json. = Secret Manager when the app runs in the Development environment.
  • Environment variables.
  • Command-line arguments.

Settings specified lower down the list override previous ones. This means that on a development machine, the connectionStrings:MyConnectionString in appsettings.Developoment.json overrides the element with the same name in appsettings.json.

This also means that we can override the connection string with an environment variabl or a command-line argument, eg

dotnet run /connectionStrings:MyConnectionString Server=localhost;port=3306;database=mydb;user=root;password=rt123;

Upvotes: 2

Related Questions