public static IServiceCollection AddCustomDbContext(this IServiceCollection services, IConfiguration configuration) { services.AddEntityFrameworkSqlServer() .AddDbContext<CatalogContext>(options => { options.UseSqlServer( ... ); }); }); return services; }
public Startup(IConfiguration configuration) { Configuration = configuration;}public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor() .AddCustomDbContext(Configuration) [ ... ] var container = new ContainerBuilder(); container.Populate(services); return new AutofacServiceProvider(container.Build()); }
public class CatalogContext : DbContext{ public CatalogContext(DbContextOptions<CatalogContext> options, IHttpContextAccessor httpContextAccessor) : base(options) { _httpContextAccessor = httpContextAccessor; } private readonly IHttpContextAccessor _httpContextAccessor; protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.AddInterceptors(new DemoInterceptor(_httpContextAccessor)); }}