418.2 Modify DBContext for the Modeling Lookup Resource of PhbkDivision of ABP framework applications - chempkovsky/CS82ANGULAR GitHub Wiki

Click to show the code
using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using rupbes.firstapp.Phbk;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace rupbes.firstapp.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class firstappDbContext :
    AbpDbContext<firstappDbContext>,
    ITenantManagementDbContext,
    IIdentityDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */


    #region Entities from the modules

    /* Notice: We only implemented IIdentityProDbContext and ISaasDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityProDbContext and ISaasDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    // Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }
    public DbSet<IdentitySession> Sessions { get; set; }

    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

    #endregion

    public firstappDbContext(DbContextOptions<firstappDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
#if (!NOTMODELING)
        builder.Entity<LprDivision02>().HasKey(p => new { p.EntrprsIdRef, p.DivisionNameIdRef, p.Id });
        builder.Entity<LprDivision01>().HasKey(p => new { p.DivisionNameIdRef, p.Id });
        builder.Entity<LpdDivision>().HasKey(p => p.DivisionNameId);
        builder.Entity<LpdDivision>()// .HasAlternateKey( p => new {p.TenantId, p.DivisionName} ).HasName("LpdDivisionNameUk");
            .HasIndex(p => new { p.TenantId, p.DivisionName }).IsUnique().HasDatabaseName("LpdDivisionNameUk");
        builder.Entity<TenantConnectionString>().HasKey(p => new { p.TenantId, p.Name });
        builder.Entity<Tenant>().HasKey(p => p.Id);
        builder.Entity<PhbkFile>().HasKey(p => new { p.FileId, p.FlNm });
        builder.Entity<PhbkPhone>().HasKey(p => p.Id);
        builder.Entity<PhbkEmployee>().HasKey(p => p.Id);
        builder.Entity<PhbkDivision>().HasKey(p => p.Id);
        builder.Entity<PhbkPhoneType>().HasKey(p => p.Id);
        builder.Entity<PhbkEnterprise>().HasKey(p => p.Id);
        builder.Entity<PhbkPhoneMtm>().HasKey(p => p.Id);
        builder.Entity<PhbkEmpToPhn>().HasKey(p => new { p.EmpIdRef, p.PhnIdRef });
#endif
        base.OnModelCreating(builder);
        /* Include modules to your migration db context */
        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureFeatureManagement();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureTenantManagement();
        builder.ConfigureBlobStoring();
        /* Configure your own tables/entities inside here */
        //builder.Entity<YourEntity>(b =>
        //{
        //    b.ToTable(firstappConsts.DbTablePrefix + "YourEntities", firstappConsts.DbSchema);
        //    b.ConfigureByConvention(); //auto configure for the base class props
        //    //...
        //});
        builder.ConfigurefirstappDbContext();
#if (!NOTMODELING)
        builder.Entity<PhbkDivision>().HasOne(d => d.Enterprise)
                .WithMany(m => m.Divisions)
                .HasForeignKey(d => d.EntrprsIdRef)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<PhbkEmployee>().HasOne(d => d.Division)
                .WithMany(m => m.Employees)
                .HasForeignKey(d => d.DivisionIdRef)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<PhbkPhone>().HasOne(d => d.PhoneType)
                .WithMany(m => m.Phones)
                .HasForeignKey(d => d.PhoneTypeIdRef)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<PhbkPhone>().HasOne(d => d.Employee)
                .WithMany(m => m.Phones)
                .HasForeignKey(d => d.EmployeeIdRef)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<PhbkPhoneMtm>().HasOne(d => d.PhoneType)
                .WithMany(m => m.PhoneMtms)
                .HasForeignKey(d => d.PhoneTypeIdRef)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<PhbkEmpToPhn>().HasOne(d => d.PhoneMtm)
                .WithMany(m => m.EmpToPhns)
                .HasForeignKey(d => d.PhnIdRef)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<PhbkEmpToPhn>().HasOne(d => d.Employee)
                .WithMany(m => m.EmpToPhns)
                .HasForeignKey(d => d.EmpIdRef)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<TenantConnectionString>().HasOne(d => d.Tnnt)
                .WithMany(m => m.ConnectionStrings)
                .HasForeignKey(d => d.TenantId)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);


        builder.Entity<LprDivision01>().HasOne(d => d.Division)
                .WithMany(m => m.DivisionRefs01)
                .HasForeignKey(d => d.Id)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<LprDivision01>().HasOne(d => d.DivisionNameDict)
                .WithMany(m => m.DivisionRef01)
                .HasForeignKey(d => d.DivisionNameIdRef)
                .HasPrincipalKey(p => p.DivisionNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<LprDivision02>().HasOne(d => d.Division)
                .WithMany(m => m.DivisionRefs02)
                .HasForeignKey(d => d.Id)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<LprDivision02>().HasOne(d => d.Enterprise)
                .WithMany(m => m.DivisionRefs02)
                .HasForeignKey(d => d.EntrprsIdRef)
                .HasPrincipalKey(p => p.Id)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        builder.Entity<LprDivision02>().HasOne(d => d.DivisionNameDict)
                .WithMany(m => m.DivisionRef02)
                .HasForeignKey(d => d.DivisionNameIdRef)
                .HasPrincipalKey(p => p.DivisionNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
#endif
    }

    public DbSet<PhbkPhoneType> PhbkPhoneTypeDbSet
    {
        get => Set<PhbkPhoneType>();

    }

    public DbSet<PhbkEnterprise> PhbkEnterpriseDbSet
    {
        get => Set<PhbkEnterprise>();

    }

    public DbSet<PhbkDivision> PhbkDivisionDbSet
    {
        get => Set<PhbkDivision>();

    }

    public DbSet<PhbkEmployee> PhbkEmployeeDbSet
    {
        get => Set<PhbkEmployee>();

    }

    public DbSet<PhbkPhone> PhbkPhoneDbSet
    {
        get => Set<PhbkPhone>();

    }

    public DbSet<PhbkFile> PhbkFileDbSet
    {
        get => Set<PhbkFile>();

    }

    public DbSet<PhbkPhoneMtm> PhbkPhoneMtmDbSet
    {
        get => Set<PhbkPhoneMtm>();

    }

    public DbSet<PhbkEmpToPhn> PhbkEmpToPhnDbSet
    {
        get => Set<PhbkEmpToPhn>();

    }

#if (!NOTMODELING)
    public DbSet<LpdDivision> LpdDivisionDbSet
    {
        get => Set<LpdDivision>();

    }

    public DbSet<LprDivision01> LprDivision01DbSet
    {
        get => Set<LprDivision01>();

    }

    public DbSet<LprDivision02> LprDivision02DbSet
    {
        get => Set<LprDivision02>();

    }
#endif

}
⚠️ **GitHub.com Fallback** ⚠️