0
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
{
    IConfigurationRoot configuration = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json")
                   .Build();

    var connectionString = configuration.GetConnectionString("DefaultConnection");

    optionsBuilder.UseSqlServer(connectionString);
}

After scaffolding the MySQL database tables, I don't want to keep the connection string in the context class. It should be fetched from the appsettings.json file.

My models generated are in separate class library.

I need a example if this can be achieved with DI pattern

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    check this https://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp – Wisitsak.San Jan 14 '23 at 16:33
  • creating the configuration in the model creating is not recommended (at least, it's not 'normal'). I would recommend creating a new project from one of the many templates, and following that, rather than trying to create something yourself. – Neil Jan 14 '23 at 17:05

0 Answers0