QlbrcDbContext.cs 2.5 KB

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