|
12345678910111213141516171819202122232425262728293031323334353637383940 |
- 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<HealthMonitorContext> 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);
- }
- }
- }
|