061 Database access for Lookup resource of PhbkEmployeeView - chempkovsky/CS82ANGULAR GitHub Wiki

Steps required to accomplish the task

  • We do not describe step by step instructions
    • please repeat the steps similar to those described in the article 040
  • LpPhBkEntity is the destination project for the Entities
    • PhBk is the destination folder for the Entities
  • LpPhBkContext is the destination project for the Dbcontext
    • PhBk is the destination folder for the Dbcontext
      • LpEmpPhBkContext will be new Dbcontext for the PhbkEmployeeView Lookup resource

LpdEmpLastName

LpdEmpFirstName

LpdEmpSecondName

LprEmployee01

  • it repeats the code of the article 056 for LprEmployee01 except for the namespace.
    • In addition one line of code must be removed from the class declaration:
        public PhbkEmployee Employee { get; set; } = null!;

LprEmployee02

  • it repeats the code of the article 056 for LprEmployee02 except for the namespace.
    • In addition two lines of code must be removed from the class declaration:
        public PhbkEmployee Employee { get; set; } = null!;
        public PhbkDivision Division { get; set; } = null!;

LpEmpPhBkContext

  • it repeats the code of the article 057 except for the extra foreign keys. The foreign keys which references PhbkDivision and PhbkEmployee for the LprEmployee02 and LprEmployee01 tables.
  • Since the code of the PhBkContext is generated by the Wizard we copied/pasted the content of the PhBkContext-file into LpEmpPhBkContext-file. We are talking about the content related to LpdEmpLastName, ..., LprEmployee01, LprEmployee02 entities. Maybe, it's better and faster than repeating the same steps with the Wizard for the second Dbcontext.
Click to show the code
using Microsoft.EntityFrameworkCore;
using LpPhBkEntity.PhBk;


namespace LpPhBkContext.PhBk
{
    public class LpEmpPhBkContext : DbContext
    {
        public LpEmpPhBkContext(DbContextOptions<LpPhbkDbContext> options)
          : base(options)
        {
            Database.EnsureCreated();
        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<LprEmployee02>().HasKey(p => new { p.DivisionIdRef, p.EmpLastNameIdRef, p.EmpFirstNameIdRef, p.EmpSecondNameIdRef, p.EmployeeId });
            modelBuilder.Entity<LprEmployee01>().HasKey(p => new { p.EmpLastNameIdRef, p.EmpFirstNameIdRef, p.EmpSecondNameIdRef, p.EmployeeId });
            modelBuilder.Entity<LpdEmpSecondName>().HasAlternateKey(p => p.EmpSecondName).HasName("LpdEmpSecondNameUK");
            modelBuilder.Entity<LpdEmpSecondName>().HasKey(p => p.EmpSecondNameId);
            modelBuilder.Entity<LpdEmpFirstName>().HasAlternateKey(p => p.EmpFirstName).HasName("LpdEmpFirstNameUK");
            modelBuilder.Entity<LpdEmpFirstName>().HasKey(p => p.EmpFirstNameId);
            modelBuilder.Entity<LpdEmpLastName>().HasAlternateKey(p => p.EmpLastName).HasName("LpdEmpLastNameUK");
            modelBuilder.Entity<LpdEmpLastName>().HasKey(p => p.EmpLastNameId);
            modelBuilder.Entity<LprEmployee02>().HasOne(d => d.EmpLastNameDict)
                .WithMany(m => m.EmployeeRef02)
                .HasForeignKey(d => d.EmpLastNameIdRef)
                .HasPrincipalKey(p => p.EmpLastNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
            modelBuilder.Entity<LprEmployee02>().HasOne(d => d.EmpFirstNameDict)
                .WithMany(m => m.EmployeeRef02)
                .HasForeignKey(d => d.EmpFirstNameIdRef)
                .HasPrincipalKey(p => p.EmpFirstNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
            modelBuilder.Entity<LprEmployee02>().HasOne(d => d.EmpSecondNameDict)
                .WithMany(m => m.EmployeeRef02)
                .HasForeignKey(d => d.EmpSecondNameIdRef)
                .HasPrincipalKey(p => p.EmpSecondNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
            modelBuilder.Entity<LprEmployee01>().HasOne(d => d.EmpLastNameDict)
                .WithMany(m => m.EmployeeRef01)
                .HasForeignKey(d => d.EmpLastNameIdRef)
                .HasPrincipalKey(p => p.EmpLastNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
            modelBuilder.Entity<LprEmployee01>().HasOne(d => d.EmpFirstNameDict)
                .WithMany(m => m.EmployeeRef01)
                .HasForeignKey(d => d.EmpFirstNameIdRef)
                .HasPrincipalKey(p => p.EmpFirstNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
            modelBuilder.Entity<LprEmployee01>().HasOne(d => d.EmpSecondNameDict)
                .WithMany(m => m.EmployeeRef01)
                .HasForeignKey(d => d.EmpSecondNameIdRef)
                .HasPrincipalKey(p => p.EmpSecondNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        }

        public DbSet<LpdEmpLastName> LpdEmpLastNameDbSet {
            get => Set<LpdEmpLastName>();

        }

        public DbSet<LpdEmpFirstName> LpdEmpFirstNameDbSet {
            get => Set<LpdEmpFirstName>();

        }

        public DbSet<LpdEmpSecondName> LpdEmpSecondNameDbSet {
            get => Set<LpdEmpSecondName>();

        }

        public DbSet<LprEmployee01> LprEmployee01DbSet {
            get => Set<LprEmployee01>();

        }

        public DbSet<LprEmployee02> LprEmployee02DbSet {
            get => Set<LprEmployee02>();

        }
    }
}

Regenerate Views

  • We do not describe step by step instructions

    • please repeat the steps similar to those described in the article 040.
  • LpEmpPhBkContext is the context to generate the Views

  • LpPhBkEntity is the Entities project to generate the Views

  • LpPhBkViews is the destination project for the Views

    • PhBk is the destination folder for the Views

Regenerate LpdEmpLastNameView and LpdEmpLastNameViewPage

Regenerate LpdEmpFirstNameView and LpdEmpFirstNameViewPage

Regenerate LpdEmpSecondNameView and LpdEmpSecondNameViewPage

Regenerate LprEmployee01View and LprEmployee01ViewPage

  • the settings are as follows:
Click to show the picture

project structure

Regenerate LprEmployee02View and LprEmployee02ViewPage

  • the settings are as follows:
Click to show the picture

project structure

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