© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
2 replies
dave1

Registering IDbCommand and autowiring and testing

i've registered the following

builder.Services.AddScoped<IDbConnection>(
    _ => new NpgsqlConnection(builder.Configuration.GetConnectionString("DefaultConnection"))
);
builder.Services.AddScoped<RegionRepository>();
builder.Services.AddScoped<IDbConnection>(
    _ => new NpgsqlConnection(builder.Configuration.GetConnectionString("DefaultConnection"))
);
builder.Services.AddScoped<RegionRepository>();


i now want to change that in my CustomWebApplicationFactory like..

            var conn = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IDbConnection));
            if (conn is null)
            {
                throw new Exception("NpgsqlConnection is not registered.");
            }
            services.Remove(conn);
            services.AddScoped<IDbConnection>(
                _ => new NpgsqlConnection(_postgreSqlContainer.GetConnectionString())
            );
            var conn = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IDbConnection));
            if (conn is null)
            {
                throw new Exception("NpgsqlConnection is not registered.");
            }
            services.Remove(conn);
            services.AddScoped<IDbConnection>(
                _ => new NpgsqlConnection(_postgreSqlContainer.GetConnectionString())
            );


but doing this the 'regions' table cannot be found, i'd presume it's not registering it correctly.

When not swapping the implementation, it finds my local db with my local appsettings connection string

the table does exist because i'm also registering AppDbContext with entity framework(and those tests are passing, i'm currently converting everything to repositories)
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

Testing practices and libraries
C#CC# / help
2y ago
Integrational testing and authentification
C#CC# / help
2y ago
❔ IAsyncEnumerable and IQueriable unit testing
C#CC# / help
4y ago