You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HealthMonitorContext.cs 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Microsoft.EntityFrameworkCore;
  2. using System.Reflection;
  3. using HealthMonitor.Core.Dal;
  4. using HealthMonitor.Core.Map;
  5. namespace HealthMonitor.Core.Context
  6. {
  7. public class HealthMonitorContext : DbContext, IWithDataSchema
  8. {
  9. public string DataSchema { get; }
  10. public HealthMonitorContext(DbContextOptions<HealthMonitorContext> options)
  11. : base(options)
  12. {
  13. DataSchema = "health_monitor";
  14. }
  15. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  16. {
  17. base.OnConfiguring(optionsBuilder);
  18. }
  19. protected override void OnModelCreating(ModelBuilder modelBuilder)
  20. {
  21. modelBuilder.HasDefaultSchema(DataSchema);
  22. var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
  23. .Where(type => !string.IsNullOrEmpty(type.Namespace) && type.Namespace.StartsWith("HealthMonitor.Core.Map.HealthMonitor"))
  24. .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(GenericEntityTypeConfiguration<>));
  25. foreach (var t in typesToRegister)
  26. {
  27. dynamic configurationInstance = Activator.CreateInstance(t)!;
  28. modelBuilder.ApplyConfiguration(configurationInstance);
  29. }
  30. base.OnModelCreating(modelBuilder);
  31. }
  32. }
  33. }