QlbrcDbContext.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Microsoft.EntityFrameworkCore;
  2. using Quadarax.Application.QLiberace.Base.Entities;
  3. using Quadarax.Application.QLiberace.Common.Attributes;
  4. using Quadarax.Application.QLiberace.Common.Configuration;
  5. using Quadarax.Foundation.Core.Data.Domain;
  6. using Quadarax.Foundation.Core.Reflection;
  7. using System.Diagnostics;
  8. using Quadarax.Application.QLiberace.Base.Entities.DaoMapper;
  9. namespace Quadarax.Application.QLiberace.Base
  10. {
  11. [DbContextModuleAssignment(Constants.Modules.Base.Code)]
  12. public class QlbrcDbContext : DataDomain
  13. {
  14. #region *** Mapped entities ***
  15. public DbSet<User> Users { get; set; } = null!;
  16. public DbSet<Tenant> Tenants { get; set; } = null!;
  17. public DbSet<Setting> Settings { get; set; } = null!;
  18. #endregion
  19. #region *** Constructors ***
  20. public QlbrcDbContext() : base(new DbContextOptions<QlbrcDbContext>())
  21. {
  22. }
  23. public QlbrcDbContext(DbContextOptions options) : base(options)
  24. {
  25. }
  26. #endregion
  27. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  28. {
  29. optionsBuilder.UseSqlServer(GetConnectionString()).LogTo(message => Debug.WriteLine("EFSQL>>" + message));;
  30. }
  31. protected string GetConnectionString()
  32. {
  33. var attr = AttributeQuery<DbContextModuleAssignmentAttribute>.Get(typeof(QlbrcDbContext));
  34. if (attr == null)
  35. throw new InvalidOperationException($"No connection string defined for DbContext {this.GetType().Name} or DbContextModuleAssignment is missing.");
  36. return OlbrcConfiguration.Get(attr.ModuleCode).ConnectionString;
  37. }
  38. #region *** Schema ***
  39. protected override void OnModelCreating(ModelBuilder modelBuilder)
  40. {
  41. new TenantDm().Map(modelBuilder);
  42. new UserDm().Map(modelBuilder);
  43. new SettingDm().Map(modelBuilder);
  44. new TenantUserDm().Map(modelBuilder);
  45. base.OnModelCreating(modelBuilder);
  46. }
  47. #endregion
  48. }
  49. }