© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•7mo ago•
5 replies
Maik

✅ ASP.NET Core DI: DbContext Not Shared in Scoped Services

I'm working on an ASP.NET Core application using Entity Framework Core. I have a single
ApplicationDbContext
ApplicationDbContext
that implements multiple interfaces, including
ISubsetDbContext
ISubsetDbContext
(for specific DbSet access) and
IUnitOfWork
IUnitOfWork
(for transaction management).

Based on my understanding, my DI setup is configured to ensure that
ISubsetDbContext
ISubsetDbContext
and
IUnitOfWork
IUnitOfWork
resolve to the very same instance of
ApplicationDbContext
ApplicationDbContext
within any given request scope. This is because both are registered with a Scoped lifetime and explicitly retrieve
ApplicationDbContext
ApplicationDbContext
via
provider.GetRequiredService<ApplicationDbContext>()
provider.GetRequiredService<ApplicationDbContext>()
.

public static IServiceCollection AddPersistence(this IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
    {
        var secretConnectionStringService = serviceProvider.GetRequiredService<ISecretConnectionStringService>();
        options.UseNpgsql(secretConnectionStringService.GetConnectionString());
    });

    
    services.AddScoped<ISubsetDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
    services.AddScoped<IUnitOfWork>(provider => provider.GetRequiredService<ApplicationDbContext>());
    
    return services;
}
public static IServiceCollection AddPersistence(this IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
    {
        var secretConnectionStringService = serviceProvider.GetRequiredService<ISecretConnectionStringService>();
        options.UseNpgsql(secretConnectionStringService.GetConnectionString());
    });

    
    services.AddScoped<ISubsetDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
    services.AddScoped<IUnitOfWork>(provider => provider.GetRequiredService<ApplicationDbContext>());
    
    return services;
}


The unexpected behavior is that when I inject
IUnitOfWork
IUnitOfWork
into a pipeline behavior and
ISubsetDbContext
ISubsetDbContext
into another service within what I believe to be the same request scope, they appear to be receiving different instances of
ApplicationDbContext
ApplicationDbContext
. This is problematic because a transaction started by
IUnitOfWork
IUnitOfWork
is then not shared with operations performed through
ISubsetDbContext
ISubsetDbContext
.

Could anyone help me out here? I might just have a big misunderstanding...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

DI in Asp.net core, abstract
C#CC# / help
2y ago
asp.net core services initialization
C#CC# / help
4y ago
.NET Core DBContext
C#CC# / help
2w ago