418.7 Entities and DbContext for Lookup Resource of ABP framework applications - chempkovsky/CS82ANGULAR GitHub Wiki

  • create LpPhBk-folder in the rupbes.firstapp.Domain.csproj-project
  • modify Directory.Build.props files (we have two files) as shown below
<Project>
    <PropertyGroup>
        <DefineConstants>XXNOTMODELING</DefineConstants>
    </PropertyGroup>
</Project>
  • in LpPhBk-folder of the rupbes.firstapp.Domain.csproj-project add three classes
  • here is LpdDivision-class
Click to show the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy;

namespace rupbes.firstapp.LpPhBk
{
    public class LpdDivision : Entity, IMultiTenant
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Display(Description = "Row id", Name = "Id of the Row", Prompt = "Enter Row Id", ShortName = "Row Id")]
        [Required]
        public int DivisionNameId { get; protected set; }

        [Display(Description = "Name of the Enterprise Division", Name = "Name of the Division", Prompt = "Enter Division Name", ShortName = "Division Name")]
        [StringLength(20, MinimumLength = 3, ErrorMessage = "Invalid")]
        [Required]
        public string DivisionName { get; set; } = null!;


        [Display(Description = "Tenant id", Name = "Tenant Id", Prompt = "Enter Tenant Id", ShortName = "Tenant Id")]
        [Description("DictHelper")]
        public virtual Guid? TenantId { get; protected set; }

        public LpdDivision() : base()
        {

        }

#if (!NOTMODELING)
        public List<LprDivision01> DivisionRef01 { get; set; } = null!;
        public List<LprDivision02> DivisionRef02 { get; set; } = null!;
#endif

        public LpdDivision(string divisionName) : base()
        {
            Check.NotNullOrWhiteSpace(divisionName, nameof(DivisionName));
            DivisionName = divisionName;
        }

        public virtual void ChangeVals(string divisionName)
        {
            Check.NotNullOrWhiteSpace(divisionName, nameof(DivisionName));
            DivisionName = divisionName;
        }

        public override object?[] GetKeys()
        {
            return new object[] { DivisionNameId };
        }

    }
}
  • here is LprDivision01-class
    • it does not reference PhbkDivision-class (!!!)
Click to show the code
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Volo.Abp.Domain.Entities;

namespace rupbes.firstapp.LpPhBk
{
    public class LprDivision01 : Entity
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Description = "Row id", Name = "Id of the Division", Prompt = "Enter Division Id", ShortName = "Division Id")]
        [Required]
        public int Id { get; set; }


        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Description = "Row id", Name = "Id of the DivisionName Row", Prompt = "Enter DivisionName Row Id", ShortName = "DivisionName Row Id")]
        [Required]
        public int DivisionNameIdRef { get; set; }

        public override object?[] GetKeys()
        {
            return new object[] { DivisionNameIdRef, Id };
        }

#if (!NOTMODELING)
        public LpdDivision DivisionNameDict { get; set; } = null!;
#endif

    }
}
  • here is LprDivision02-class
    • it does not reference PhbkDivision-class (!!!)
    • it does not reference PhbkEnterprise-class (!!!)
Click to show the code
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Volo.Abp.Domain.Entities;

namespace rupbes.firstapp.LpPhBk
{
    public class LprDivision02 : Entity
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Description = "Row id", Name = "Id of the Division", Prompt = "Enter Division Id", ShortName = "Division Id")]
        [Required]
        public int Id { get; set; }


        [Display(Description = "Row id", Name = "Id of the Enterprise", Prompt = "Enter Enterprise  Id", ShortName = "Enterprise Id")]
        [Required]
        public int EntrprsIdRef { get; set; }


        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Description = "Row id", Name = "Id of the DivisionName Row", Prompt = "Enter DivisionName Row Id", ShortName = "DivisionName Row Id")]
        [Required]
        public int DivisionNameIdRef { get; set; }

        public override object?[] GetKeys()
        {
            return new object[] { EntrprsIdRef, DivisionNameIdRef, Id };
        }

#if (!NOTMODELING)
        public LpdDivision DivisionNameDict { get; set; } = null!;
#endif

    }
}
  • using DbContext Wizard modify LpPhbkDbContext-class so it becomes as follows:
Click to show the code
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using rupbes.firstapp.LpPhBk;



namespace rupbes.firstapp.EntityFrameworkCore
{
    [ConnectionStringName("LpphbkConnStr")]
    public class LpPhbkDbContext : AbpDbContext<LpPhbkDbContext>
    {
        public LpPhbkDbContext(DbContextOptions<LpPhbkDbContext> 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>()// .HasAlternateKey( p => new {p.TenantId, p.DivisionName} ).HasName("LpdDivisionNameUk");
                .HasIndex(p => new { p.DivisionName }).IsUnique().HasDatabaseName("LpdDivisionNameUk");
            builder.Entity<LpdDivision>().HasKey(p => p.DivisionNameId);
#endif
            base.OnModelCreating(builder);
            builder.ConfigureLpPhbkDbContext();

#if (!NOTMODELING)
            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.DivisionNameDict)
                .WithMany(m => m.DivisionRef02)
                .HasForeignKey(d => d.DivisionNameIdRef)
                .HasPrincipalKey(p => p.DivisionNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
#endif
        }

        public DbSet<LpdDivision> LpdDivisionDbSet
        {
            get => Set<LpdDivision>();

        }

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

        }

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

        }
    }
}
  • modify LpPhbkDbContextCreatingExtensions-class as follows
Click to show the code
using Microsoft.EntityFrameworkCore;
using rupbes.firstapp.LpPhBk;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace rupbes.firstapp.EntityFrameworkCore
{
    public static class LpPhbkDbContextCreatingExtensions
    {
        public static void ConfigureLpPhbkDbContext(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            builder.Entity<LpdDivision>(b =>
            {
                b.ToTable(firstappConsts.DbTablePrefix + "LpdDivisions", firstappConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
                                           //...
                b.HasKey(p => p.DivisionNameId);
                b.HasIndex(p => new { p.TenantId, p.DivisionName }).IsUnique().HasDatabaseName("LpdDivisionNameUk");
            });

            builder.Entity<LprDivision01>(b =>
            {
                b.ToTable(firstappConsts.DbTablePrefix + "LprDivision01s", firstappConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
                                           //...
                b.HasKey(p => new { p.DivisionNameIdRef, p.Id });
                //b.Entity<LprDivision01>().HasOne(d => d.DivisionNameDict)
                
                b.HasOne<LpdDivision>()
                    //.WithMany(m => m.DivisionRef01)
                    .WithMany()
                    .HasConstraintName("LprDivision01LpdDivision")
                    .HasForeignKey(d => d.DivisionNameIdRef)
                    .HasPrincipalKey(p => p.DivisionNameId)
                    .IsRequired(true)
                    .OnDelete(DeleteBehavior.NoAction);
                
            });

            builder.Entity<LprDivision02>(b => {
                b.ToTable(firstappConsts.DbTablePrefix + "LprDivision02s", firstappConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
                                           //...
                b.HasKey(p => new { p.EntrprsIdRef, p.DivisionNameIdRef, p.Id });
                
                b.HasOne<LpdDivision>()
                    //.WithMany(m => m.DivisionRef01)
                    .WithMany()
                    .HasConstraintName("LprDivision02LpdDivision")
                    .HasForeignKey(d => d.DivisionNameIdRef)
                    .HasPrincipalKey(p => p.DivisionNameId)
                    .IsRequired(true)
                    .OnDelete(DeleteBehavior.NoAction);
                
            });
        }
    }
}
⚠️ **GitHub.com Fallback** ⚠️