33using Avolutions . Baf . Core . Entity . Interceptors ;
44using Avolutions . Baf . Core . Lookups . Interceptors ;
55using Avolutions . Baf . Core . Module . Abstractions ;
6+ using Avolutions . Baf . Core . Persistence ;
67using Microsoft . EntityFrameworkCore ;
78using Microsoft . Extensions . DependencyInjection ;
89using Microsoft . Extensions . DependencyInjection . Extensions ;
@@ -33,6 +34,15 @@ public static class ServiceCollectionExtensions
3334 public static IServiceCollection AddBafCore < TContext > ( this IServiceCollection services , params Assembly [ ] assemblies )
3435 where TContext : BafDbContext
3536 {
37+ // Register a factory alias so framework services (e.g., EntityService<T>) can depend on
38+ // IDbContextFactory<BafDbContext> without knowing the concrete context type.
39+ // This is the factory equivalent of the scoped DbContext alias below.
40+ services . TryAddScoped < IDbContextFactory < BafDbContext > > ( sp =>
41+ new BafDbContextFactoryWrapper < TContext > ( sp . GetRequiredService < IDbContextFactory < TContext > > ( ) ) ) ;
42+
43+ // Keep scoped aliases for backward compatibility.
44+ // AddDbContextFactory already registers TContext as scoped, so API controllers
45+ // and other non-Blazor code that injects DbContext/BafDbContext directly still works.
3646 services . TryAddScoped < DbContext > ( sp => sp . GetRequiredService < TContext > ( ) ) ;
3747 services . TryAddScoped < BafDbContext > ( sp => sp . GetRequiredService < TContext > ( ) ) ;
3848
@@ -48,7 +58,7 @@ public static IServiceCollection AddBafCore<TContext>(this IServiceCollection se
4858 services . AddSingleton ( new BafRegistry ( modules , moduleAssemblies ) ) ;
4959
5060 // Add database context interceptors
51- services . AddDbContext < TContext > ( ( sp , options ) =>
61+ services . AddDbContextFactory < TContext > ( ( sp , options ) =>
5262 {
5363 options . AddInterceptors (
5464 sp . GetRequiredService < AuditSaveChangesInterceptor > ( ) ,
0 commit comments