A named connection string was used, but the name ‘tumoDB’ was not found in the application’s configuration. .Net core 3.0 REST API

Hi Could you please assist,i have run a scafold from my database using the below PM command and it works fine and the DB context is created.

Scaffold-DbContext "Data Source=(localhost);Initial Catalog=tumoDB;User ID=tumo;password=****" Microsoft.EntityFrameworkCore.SqlServer -OutputDir InfrastructureModels -Tables BnplIndicator -f

But when i try to follow .Net security principles by placing th the connectionstring in appsettings.json,i get the error in the title that the name of my DB(tumoDB) is not found,please see my connection strings bellow.

 "ConnectionStrings": {
    "MySql": {
      "PhoneAppDB": {
        "ConnString": "Server=(localhost)Sql1;uid=tumo;pwd=****;Convert Zero Datetime=True;",
        // "ConnString": "Server=(localhost)Sql2;port=3030;Database=MohemStores;user=root;password=***;",
        "Timeout": 60000,
        "MaxRetries": 6
      }
    },
    "SqlServer": {
      "tumoDB": {
        "ConnString": "Data Source=(localhost);Initial Catalog=tumoDB;User ID=tumo;Password=***;",
        "Timeout": 60000,
        "MaxRetries": 5
      }
    }
  },
  ...

and now i use this below i get the error:tumoDB was not found in application configuration

Scaffold-DbContext -Connection Name=tumoDB Microsoft.EntityFrameworkCore.SqlServer -OutputDir InfrastructureModels -Tables BnplIndicator -force

I researched and even added the code in the Iconfiguration in Startup.cs as recommended but still getting the same error,please note all required packages (EF.Sqlserver and EF.tools) are installed properly and the API runs it is just the command to scafold DBcontext using the name that gives problems.

   services.AddCustomDataStoreConfiguration(Configuration);
            services.AddDbContext<tumoDBContext>(options =>
                 options.UseSqlServer(Configuration.GetConnectionString("tumoDB")));

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I actually found what the answer was,you cannot create a sub connection string under another connection string.

Solution:

 "ConnectionStrings": {
   
      "PhoneAppDB":"Server=(localhost)Sql1;uid=tumo;pwd=****;Convert Zero Datetime=True;",   
  
      "tumoDB": 
        "ConnString": "Data Source=(localhost);Initial Catalog=tumoDB;User ID=tumo;Password=***;"
       
  },


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x