Class ServiceCollectionDbProviderExtensions
Extension methods for IServiceCollection that simplify replacing EF Core database provider registrations in integration tests.
public static class ServiceCollectionDbProviderExtensions
- Inheritance
-
ServiceCollectionDbProviderExtensions
- Inherited Members
Methods
ReplaceDbProvider<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>)
Removes all existing EF Core provider registrations for TContext
and re-registers with a new provider configuration via AddDbContext. Use this in
WebApplicationFactory tests to swap a production database provider
(e.g., SQL Server) for a lightweight test provider (e.g., SQLite in-memory).
public static IServiceCollection ReplaceDbProvider<TContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> configureOptions) where TContext : DbContext
Parameters
servicesIServiceCollectionThe service collection to modify.
configureOptionsAction<DbContextOptionsBuilder>An action to configure the new DbContextOptionsBuilder.
Returns
- IServiceCollection
The same IServiceCollection for chaining.
Type Parameters
TContextThe DbContext type to replace.
Examples
builder.ConfigureServices(services =>
services.ReplaceDbProvider<AppDbContext>(options =>
options.UseSqlite(connection).AddTrellisInterceptors()));
Remarks
This method always re-registers using AddDbContext<TContext>.
If the application registers the context via AddDbContextFactory or
AddPooledDbContextFactory, use those APIs directly instead of this helper.