0

This is a follow up to a question I asked previously, regarding how to make a DBContext work with different connection strings. It turns out such a thing is possible, but I'm not sure how to modify my project to take advantage of it.

The following refers to a project using Entity Framework Core 6.0.7, the latest version as of this writing, and targeting .NET 6, for the record.

I see from Microsoft documentation that I can construct a DBContext to take a declared connection string, and a boolean indicating whether "[the] connection will be disposed when the context is disposed". This sounds like what I want, so I have altered the constructor for my DBContext from this:

public GenericAppContext()
{
            
}

public GenericAppContext(DbContextOptions<GenericAppContext> options)
    : base(options)
{
}

to this:

public GenericAppContext(string connectionStringName, bool ownsConnection)
{
    DbConnection conn = //declare the connection based on the given connection string name, somehow
}

I also have a Generic App Repository, which will use this context for its database calls. Currently, the repo is instantiating the context in the usual, default way:

private readonly GenericAppContext _context;

public GenericAppRepository(GenericAppContext context)
{
    _context = context;
}  

My actual connection strings are all declared in my appsettings.json file. Where I'm stuck is how to declare my generic context in my program.cs file, since obviously this isn't going to work...

builder.Services.AddDbContext<GenericAppContext>(options =>
                options.UseSqlServer(builder.Configuration.GetConnectionString("GeneralDataConnection")));

...because it declares an explicit connection. Do I even need to do this under these circumstances?

And then, how do I build my DbConnection from the given connection string name?

bmurrell30
  • 565
  • 8
  • 23
  • Remove AuthenticationContext and replace it with your context: builder.Services.AddDbContext(...); – GH DevOps Jul 20 '22 at 14:42
  • The standard convention is 1) map connection strings to a DB context in your `appsettings.json`. 2) Define different "environments". 3) Your code will use one DBContext. The runtime will connect to different databases as needed. Look here for more details: https://stackoverflow.com/a/67806781/421195 – paulsm4 Jul 20 '22 at 14:45
  • It sounds like you won't know which connection you're planning to use until runtime: I'm guessing it's derived based on a parameter sent to your controller action or something? Rather than using a DI binding for the GenericAppContext directly, You'll need to use factories/providers to create your app context at the point where you know which connection to use. You can inject the factory, and then ask it for the context that applies based on the given arguments. You can either do this at your repository layer, or use a separate factory to produce the repository too. – StriplingWarrior Jul 20 '22 at 16:45
  • And your new constructor should pass its argument through to the base constructor. `public GenericAppContext(string connectionStringName, bool ownsConnection) : base(connectionStringName, ownsConnection)` – StriplingWarrior Jul 20 '22 at 16:47

0 Answers0