I have an application written in Core 2.0, using EF Core. I'm using a pattern where:
- I keep my database connection string in Azure Keyvault.
- I use program.cs to read a (.gitignore'd) json file with my Keyvault appid/password, go to the Keyvault, and put my sql connection string into Configuration.
- DI handles everything from there.
My problem is that when doing scaffolding (create a view from a model, even one that doesn't have anything to do with EF models, or creating a controller from a model), Startup.cs/Program.cs don't seem to run so no DI.
Since there's no connection strings around, I get failures when scaffolding. If I add the connection string into a parameterless constructor on my data context class, it works. I can take it back out before I commit my code, but it seems really dangerous.
In short:
Using the DI method of adding db configuration strings (and not having them written into your application) how do you get scaffolding to work?
Edit, some code. This is what normally runs in my application, but (I think?) because DI doesn't run during scaffolding then this constructor doesn't get called.
public monosarsqlContext(IConfiguration config)
{
this.m_sqlConnectioNString = config["sqlconnectionstring"];
}
To get scaffolding to work, I'm currently needing to copy/paste the connection string into this parameterless constructor. Again, it works, but I'm a fat-finger commit away from pushing my database connection string into a public github repo.
public monosarsqlContext()
{
this.m_sqlConnectioNString =
"Server=mycooldatabase.domain.com;Database=soradcool;
Persist Security Info=False;User ID=coolguy;Password=coolpassword";
//only here for scaffolding, do not use
throw new NotImplementedException("DBContext only used for
scaffolding,
make use of DI method.");
}