QlbrcDbContext.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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.Reflection;
  6. namespace Quadarax.Application.QLiberace.Base
  7. {
  8. [DbContextModuleAssignment(Constants.Modules.Base.Code)]
  9. public class QlbrcDbContext : DbContext
  10. {
  11. #region *** Mapped entities ***
  12. public DbSet<User> Users { get; set; }
  13. public DbSet<Tenant> Tenants { get; set; }
  14. public DbSet<Setting> Settings { get; set; }
  15. #endregion
  16. #region *** Constructors ***
  17. public QlbrcDbContext() : base()
  18. {
  19. }
  20. public QlbrcDbContext(DbContextOptions options) : base(options)
  21. {
  22. }
  23. #endregion
  24. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  25. {
  26. optionsBuilder.UseSqlServer(GetConnectionString());
  27. }
  28. private string GetConnectionString()
  29. {
  30. var attr = AttributeQuery<DbContextModuleAssignmentAttribute>.Get(typeof(QlbrcDbContext));
  31. if (attr == null)
  32. throw new InvalidOperationException($"No connection string defined for DbContext {this.GetType().Name} or DbContextModuleAssignment is missing.");
  33. return OlbrcConfiguration.Get(attr.ModuleCode).ConnectionString;
  34. }
  35. }
  36. }