QlbrcDbContext.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. public DbSet<TenantUser> TenantUsers { get; set; } = null!;
  19. #endregion
  20. #region *** Constructors ***
  21. public QlbrcDbContext() : base(new DbContextOptions<QlbrcDbContext>())
  22. {
  23. }
  24. public QlbrcDbContext(DbContextOptions options) : base(options)
  25. {
  26. }
  27. #endregion
  28. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  29. {
  30. optionsBuilder.UseSqlServer(GetConnectionString()).LogTo(message => Debug.WriteLine("EFSQL>>" + message));;
  31. }
  32. protected string GetConnectionString()
  33. {
  34. var attr = AttributeQuery<DbContextModuleAssignmentAttribute>.Get(typeof(QlbrcDbContext));
  35. if (attr == null)
  36. throw new InvalidOperationException($"No connection string defined for DbContext {this.GetType().Name} or DbContextModuleAssignment is missing.");
  37. return OlbrcConfiguration.Get(attr.ModuleCode).ConnectionString;
  38. }
  39. #region *** Schema ***
  40. protected override void OnModelCreating(ModelBuilder modelBuilder)
  41. {
  42. new TenantDm().Map(modelBuilder);
  43. new UserDm().Map(modelBuilder);
  44. new SettingDm().Map(modelBuilder);
  45. new TenantUserDm().Map(modelBuilder);
  46. base.OnModelCreating(modelBuilder);
  47. }
  48. #endregion
  49. }
  50. }