using Microsoft.EntityFrameworkCore; using Quadarax.Application.QLiberace.Base.Entities; using Quadarax.Application.QLiberace.Common.Attributes; using Quadarax.Application.QLiberace.Common.Configuration; using Quadarax.Foundation.Core.Data.Domain; using Quadarax.Foundation.Core.Reflection; using System.Diagnostics; using Quadarax.Application.QLiberace.Base.Entities.DaoMapper; namespace Quadarax.Application.QLiberace.Base { [DbContextModuleAssignment(Constants.Modules.Base.Code)] public class QlbrcDbContext : DataDomain { #region *** Mapped entities *** public DbSet Users { get; set; } = null!; public DbSet Tenants { get; set; } = null!; public DbSet Settings { get; set; } = null!; public DbSet TenantUsers { get; set; } = null!; public DbSet TenantCurrencies { get; set; } = null!; public DbSet TenantCountries { get; set; } = null!; #endregion #region *** Constructors *** public QlbrcDbContext() : base(new DbContextOptions()) { } public QlbrcDbContext(DbContextOptions options) : base(options) { } #endregion protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(GetConnectionString()).LogTo(message => Debug.WriteLine("EFSQL>>" + message));; } protected string GetConnectionString() { var attr = AttributeQuery.Get(typeof(QlbrcDbContext)); if (attr == null) throw new InvalidOperationException($"No connection string defined for DbContext {this.GetType().Name} or DbContextModuleAssignment is missing."); return OlbrcConfiguration.Get(attr.ModuleCode).ConnectionString; } #region *** Schema *** protected override void OnModelCreating(ModelBuilder modelBuilder) { new TenantDm().Map(modelBuilder); new UserDm().Map(modelBuilder); new SettingDm().Map(modelBuilder); new CurrencyDm().Map(modelBuilder); new CountryDm().Map(modelBuilder); new TenantUserDm().Map(modelBuilder); new TenantCurrencyDm().Map(modelBuilder); new TenantCountryDm().Map(modelBuilder); base.OnModelCreating(modelBuilder); } #endregion } }