using Microsoft.EntityFrameworkCore; using System.Reflection; using HealthMonitor.Core.Dal; using HealthMonitor.Core.Map; namespace HealthMonitor.Core.Context { public class HealthMonitorContext : DbContext, IWithDataSchema { public string DataSchema { get; } public HealthMonitorContext(DbContextOptions options) : base(options) { DataSchema = "health_monitor"; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema(DataSchema); var typesToRegister = Assembly.GetExecutingAssembly().GetTypes() .Where(type => !string.IsNullOrEmpty(type.Namespace) && type.Namespace.StartsWith("HealthMonitor.Core.Map.HealthMonitor")) .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(GenericEntityTypeConfiguration<>)); foreach (var t in typesToRegister) { dynamic configurationInstance = Activator.CreateInstance(t)!; modelBuilder.ApplyConfiguration(configurationInstance); } base.OnModelCreating(modelBuilder); } } }