1

I'm trying to register multiple DbContext implementations in aspnet core DI

so I registered DbContext as bellow

services.AddScoped(c => new CoreDbContext(c.GetService<DbContextOptions<CoreDbContext>>()));
services.AddScoped(c => new TnADbContext(c.GetService<DbContextOptions<TnADbContext>>()));
services.AddScoped<Func<DbContextType, IDbContext>>(provider => key =>
{
    switch (key)
    {
        case DbContextType.Core:
            return provider.GetService<CoreDbContext>();
        case DbContextType.TnA:
            return provider.GetService<TnADbContext>();
        case DbContextType.Payroll:
            throw new ArgumentOutOfRangeException(nameof(key), key, null);
        default:
            throw new ArgumentOutOfRangeException(nameof(key), key, null);
    }
});

so from repositories, I'm trying to request instance like below

private readonly IDbContext _context;
public Repository(Func<DbContextType, IDbContext> resolver)
{
    _context = resolver(DbContextType.TnA);
}

But when I run the application it throws an exception as below

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Web.Areas.TestController Lifetime: Transient ImplementationType: Web.Areas.TestController': Unable to resolve service for type 'Data.IDbContext' while attempting to activate 'Service.InterfaceService'.)

Basically almost all services and controller complaining about the same issue. So what was the missing part?

UPDATE

Actually I made some changes on registering DB context now it work

services.AddScoped<Func<DbContextType, IDbContext>>(provider => key =>
{
    switch (key)
    {
        case DbContextType.Core:
            return new CoreDbContext(provider.GetService<DbContextOptions<CoreDbContext>>());
        case DbContextType.TnA:
            return new TnADbContext(provider.GetService<DbContextOptions<TnADbContext>>());
        case DbContextType.Payroll:
            throw new ArgumentOutOfRangeException(nameof(key), key, null);
        default:
            throw new ArgumentOutOfRangeException(nameof(key), key, null);
    }
});
4
  • I think you missed registering the IDbContext with the IoC container. Commented Apr 24, 2020 at 23:59
  • Thank you very much for your response. I figured out the issue. The issue was I have one service still directly inject IDbContext. When i replace the injection with Func<DbContextType, IDbContext> resolver and resolve using var context = resolver(DbContextType.TnA); It worked perfectly Thanks Commented Apr 25, 2020 at 0:10
  • 1
    Great that you got it working :) Could you add an answer so other people with a similar problem can see this as a resolved question? Commented Apr 25, 2020 at 6:03
  • Yes i will add updated section as answer Commented Apr 25, 2020 at 18:00

1 Answer 1

1

So I came up with solution registering Dbconfigs as below

services.AddScoped<Func<DbContextType, IDbContext>>(provider => key =>
{
    switch (key)
    {
        case DbContextType.Core:
            return new CoreDbContext(provider.GetService<DbContextOptions<CoreDbContext>>());
        case DbContextType.TnA:
            return new TnADbContext(provider.GetService<DbContextOptions<TnADbContext>>());
        case DbContextType.Payroll:
            throw new ArgumentOutOfRangeException(nameof(key), key, null);
        default:
            throw new ArgumentOutOfRangeException(nameof(key), key, null);
    }
});

And Make sure IDbContext not to inject any services instead you can try as below

private readonly IDbContext _context;

public InterfaceService(Func<DbContextType, IDbContext> resolver)
{
    _context = resolver(DbContextType.TnA);
}

DbContext type would be an enum that contains the type of DB context that I need to inject

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.