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.

43 lines
1.2KB

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