QlbrcDbContext.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. base.OnModelCreating(modelBuilder);
  45. }
  46. #endregion
  47. }
  48. }