using Microsoft.EntityFrameworkCore; using System; using System.Linq; using System.Reflection; using HealthMonitor.Core.Dal; using HealthMonitor.Core.Map; namespace HealthMonitor.Core.Context { public class GpsCardContext : DbContext, IWithDataSchema { public string DataSchema { get; } public GpsCardContext(DbContextOptions options) : base(options) { DataSchema = "gps_card"; } 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.GpsCard")) .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); } } }