C
C#12mo ago
GABRIEL22

How to fix " The ConnectionString property has not been initialized." in .Net api

Hello world! I'm developing a .Net api using Docker containers, but at the moment to ask a GET request in swagger, it throws back " The ConnectionString property has not been initialized." which has to do with a database failure. If Anyone could bring me some help, I'd be really grateful.
No description
This is the  database setup in docker-compose file
This is the connection string in AppSettings.cs
This is the the dbcontext injection in program.cs
And fanally, the DbContext with model builder.
6 Replies
kermit
kermit12mo ago
Try this: builder.Configuration.GetConnectionString("DefaultConnection")
GABRIEL22
GABRIEL22OP12mo ago
I already tried, and it didn't workout as expected.
kermit
kermit12mo ago
Some points that I noticed in the provided screenshots: 1. If the database is on your local machine then Host will be localhost in the connection string. 2. I can't see OnConfiguring method in your Context file
GABRIEL22
GABRIEL22OP12mo ago
No, I'm using GitHub Codespaces which is a online ide. Should I set the string config in db context rather than Program.cs alone. I didnt use OnConfiguring method because I though setting the Postgres connection string in Program.cs was enough.
Mursel
Mursel12mo ago
@GABRIEL22 Maybe that your ConnectionString is inside the logging brackets in the json file? Try to put it outside the logging
kermit
kermit12mo ago
Yes I think that's the issue.

Did you find this page helpful?