C
C#2y ago
Kris

❔ Host Can't be Null error when trying to run db migration using postgres

I'm pretty sure this is a id10t issue here. I have added appsettings.json and appsettings.Development.json in all of my directories because I think my program is not referencing the right appsettings.json, however when I did I am still getting the error.
14 Replies
Kris
KrisOP2y ago
My appsettings and appsettings.Development.json have the same content
Kris
KrisOP2y ago
undisputed world champions
it seems like you don't have a appsettings.json in your Web-project that's the one that counts not sure if it makes sense having appsettings.json in library-project catthinking
Kris
KrisOP2y ago
Idk if this helps but I am running dotnet ef database update in my Helpers directory. That's where my Data Context.cs is. I readded appsettings.json and apsettings.Development.json in the Web ( realized it's missing in that screenshot ) and am getting the same results
Kris
KrisOP2y ago
This is my file structure @icebear42
Kris
KrisOP2y ago
I also tried adding the connection string directly in Program.cs via below and still getting the same error
phaseshift
phaseshift2y ago
I think since you split the context out from the web host you need to specify extra stuff in the command e.g. if you run from the the context location you need to specify the other project (-p flag) vice versa would need the -c flag to specify the name of the context as well? But not really sure - just reading chat gpt
Kris
KrisOP2y ago
Ahh will try that
Kris
KrisOP2y ago
I threw up on the verbose flag this is giving me clues
Kris
KrisOP2y ago
Adding the connection string directly into the method worked ughh.. The mystery continues!
Kris
KrisOP2y ago
The connection string is right, I think the location of the appconfig is throwng it off
blueberriesiftheywerecats
You dont need OnConfiguring, you need to pass through ctor smth like IDbContextOptions/IDbContextOptionsBuilder(i forgor)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Stage
Stage4mo ago
Reviving this just to tell anyone who stumbles across this like I did: The only thing that fixed this for me was to remove my design-time DbContextFactory implementation, which had a default constructor. A tell-tale sign you have the same issue is to run dotnet ef database update with a valid connection string as a CLI argument: --connection "<connection str>". The key is to get rid of any design-time factories / ways a DbContext can be instantiated without valid credentials. Hope this helps! (This assumes you have an otherwise valid configuration etc.)

Did you find this page helpful?