#nullable enable using System; using System.Data.Common; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; namespace BTCPayServer.Data; public static partial class ApplicationDbContextExtensions { public static DbContext GetDbContext(this DbSet dbSet) where T : class { var infrastructure = dbSet as IInfrastructure; var serviceProvider = infrastructure.Instance; var currentDbContext = (ICurrentDbContext)serviceProvider.GetService(typeof(ICurrentDbContext))!; return currentDbContext.Context; } public static DbConnection GetDbConnection(this DbSet dbSet) where T : class => dbSet.GetDbContext().Database.GetDbConnection(); }