0

ENV: VS 2019, C# .NET Framework 4.6.1

I'm using a .json file to hold all my configurations:

IConfigurationBuilder config = new ConfigurationBuilder();
        config.AddJsonFile("autofac.json");

My question is, how do I include the DbContext in this file? I'm adding Autofac to an existing application and using it in a new section. This is what the code looks like (change the real name to "My*" to protect the innocent) in the original code:

public partial class MyDbContext : DbContext, IDisposable
{
    public MyDbContext () : base("MyDB")
    {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {}
}

Is it possible to relate the DbContext concrete class to an existing interface (does one exist) or should I create a wrapper around it and then create a corresponding interface for the wrapper class?

Kit
  • 20,354
  • 4
  • 60
  • 103
MyDisplayName
  • 223
  • 5
  • 12

1 Answers1

0

The typical practice is to put connection info (e.g. a connection string) into the configuration file, not the JSON representation of a service class such as a DbContext).

You can then inject the concrete context (or, better, an IDbContextFactory that creates the MyDbContext.

It becomes very problematic very quickly to provide non-configuration things in a configuration file (e.g. repos, service classes, EF resources, etc.) Stick to configuration alone.

Kit
  • 20,354
  • 4
  • 60
  • 103