0

It's very unclear how to do this.

I have seen examples that say you can use IConfiguration, some that manually load the secrets.json (but if that's the case, why does it insert metadata into the csproj specifically about secret stores?).

Either way, within my EF context class, the only object I have is DbContextOptionsBuilder

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
               optionsBuilder.UseSqlServer(myConnectionStringGoesHere);

Can anyone offer any advice?

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • Use `AddDbContext` instead of `OnConfiguring` for actual configuration. In your `Startup` class or `Program.cs` you have access to the actual configuration object – Panagiotis Kanavos Jul 20 '22 at 11:11
  • `secrets.json` isn't some kind of special feature. The secrets Configuration provider is one more Configuration provider that happens to read its settings from a specific location outside your Git repo. It's not a highly secure provider. Other configuration providers can read settings from key management systems like AWS KMS, Azure Keyvault etc. Your own code doesn't need to know this, only the config setup code needs to change – Panagiotis Kanavos Jul 20 '22 at 11:14
  • @PanagiotisKanavos the comment in the scaffolded code says to use the optionsBuilder, are you saying to ignore that and do something else? How do I get it to read the secrets.json? And put the two together? I seriously cannot find any good documentation to do this. – NibblyPig Jul 20 '22 at 11:17
  • On the contrary, all the docs show what I wrote. All ASP.NET Core doc samples and tutorials show using `AddDbContext`. Check for example the [Add Model](https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-6.0&tabs=visual-studio) page in the `ASP.NET Core Getting Started` tutorial. The *EF* scaffolder doesn't know whether it will be used together with a Host though, that's why it adds this code. The scaffolder needs a configured DbContext to work too. If you run `dotnet ef` in a project's root folder, it will instantiate `Startup` and read the settings. – Panagiotis Kanavos Jul 20 '22 at 11:28
  • Revere engineering in general isn't as well documented as forward engineering - generating tables from contexts. Forward engineering is far more common in the small web sites built by beginners. In such sites it's possible to update the database from changes in your model without affecting other applications. – Panagiotis Kanavos Jul 20 '22 at 11:31
  • Using named connection strings is mentioned in the [Reverse Engineering](https://docs.microsoft.com/en-us/ef/core/managing-schemas/scaffolding?tabs=dotnet-core-cli#configuration-and-user-secrets) page but you have to know how things work to understand what this shows. That two-sentence section is storing a new connection string in secrets.json. The project is *already* using Configuration and `secrets.json`, so when you tell `dotnet ef` to use a specific connection name, it will ask for it from the Config middleware and get the value just stored in `secrets`. It would work even without secrets – Panagiotis Kanavos Jul 20 '22 at 11:37
  • `AddDbContext` belongs to `var builder = WebApplication.CreateBuilder(args);`, I am not always using the DB in a web application. This code doesn't really explain what goes into OnConfiguring, if I have to omit it or put some custom code in. – NibblyPig Jul 20 '22 at 11:40
  • I have tried scaffolding using the `Name=` but I get `missing required argument provider`, all I did was replace the connection string part of my scaffold command with `name=ConnectionStrings:MyConnectionString` the rest is the same as before – NibblyPig Jul 20 '22 at 11:46
  • `secrets.json` is used by `builder`. It's a .NET Configuration feature, not an EF Core feature. You can't just use it inside a DbContext. The scaffolder uses the .NET Configuration middleware, not the `secrets.json` file. The code in `OnConfiguring` is just a default configuration in case you create a DbContext using your own default constructor. – Panagiotis Kanavos Jul 20 '22 at 11:46
  • This is why I am asking... how do I use it? Because even if I can get it perhaps by manually reading the secrets.json and parsing it myself, I can't get it inside the OnConfiguring method – NibblyPig Jul 20 '22 at 11:48
  • I fiddled with it a bit and now I get this `System.InvalidOperationException: A named connection string was used, but the name 'ConnectionStrings:MyConnectionString was not found in the application's configuration. Note that named connection strings are only supported when using 'IConfiguration' and a service provider, such as in a typical ASP.NET Core application. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.` Of course, my database is not in that kind of solution, it's in a class library. Also no explanation of why it can't see the secrets.json, it's right there. – NibblyPig Jul 20 '22 at 12:00

0 Answers0