040 Database access for Lookup resource of PhbkDivisionView - chempkovsky/CS82ANGULAR GitHub Wiki

Notes

  • In the article 035 we created three Entities, but at the end we removed them from the project. Those Entities were used more the modeling only.
  • Now it's time to create the entities and DbContext that will be used by the lookup resource to access the database.
    • LpPhBkEntity-project will be used for the entities
    • LpPhBkContext-project will be used for DbContext
  • We are going to create additional DbContext.
    • A new Wizard's Repo will be created for such a DbContext as soon as we generate the first ViewModel for any newly declared entity.
  • The names and scalar properties of the newly declared entities must be the same as those of the entities created for modeling.

Steps required to accomplish the task

LpdDivision entity

  • with Visual Studio 2022 add LpdDivision.cs-file (class file) in the PhBk-folder of LpPhBkEntity-project
Click to show the code
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace LpPhBkEntity.PhBk
{
    public class LpdDivision
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Display(Description = "Row id", Name = "Id of the Row", Prompt = "Enter Row Id", ShortName = "Row Id")]
        [Required]
        public int DivisionNameId { get; 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!;

        public List<LprDivision01> DivisionRef01 { get; set; } = null!;
        public List<LprDivision02> DivisionRef02 { get; set; } = null!;
    }
}

LprDivision01 entity

  • with Visual Studio 2022 add LprDivision01.cs-file (class file) in the PhBk-folder of LpPhBkEntity-project
Click to show the code
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

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

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

        public LpdDivision DivisionNameDict { get; set; } = null!;
    }
}
  • Note: the following two line of code is gone!!!
    • public PhbkDivision Division { get; set; } = null!;

LprDivision02 entity

  • with Visual Studio 2022 add LprDivision02.cs-file (class file) in the PhBk-folder of LpPhBkEntity-project
Click to show the code
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace LpPhBkEntity.PhBk
{
    public class LprDivision02
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Description = "Row id", Name = "Id of the Division", Prompt = "Enter Division Id", ShortName = "Division Id")]
        [Required]
        public int DivisionId { 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 LpdDivision DivisionNameDict { get; set; } = null!;

    }
}
  • Note: the following two lines of code are gone!!!
    • public PhbkDivision Division { get; set; } = null!;
    • public PhbkEnterprise Enterprise { get; set; } = null!;

LpPhbkDbContext

  • right click PhBk-folder of the LpPhBkContext-project and run DbContext Wizard.

    • name the newly created DbContext as LpPhbkDbContext
    • add to LpPhbkDbContext
      • LpdDivision entity which is created in the LpPhBkEntity-project
      • LprDivision01 entity which is created in the LpPhBkEntity-project
      • LprDivision02 entity which is created in the LpPhBkEntity-project
  • Here s a result

Click to show the code
using Microsoft.EntityFrameworkCore;
using LpPhBkEntity.PhBk;


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

        //////////////////////////////////////////////////////////////////
        /// reset ".UseSqlServer"
        /// reset "ConfigurationManager.ConnectionStrings"
        /// optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        /////////////////////////////////////////////////////////////////
        // protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        // {
        //    optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        // }



        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<LprDivision02>().HasKey(p => new { p.EntrprsIdRef, p.DivisionNameIdRef, p.DivisionId });
            modelBuilder.Entity<LprDivision01>().HasKey(p => new { p.DivisionNameIdRef, p.DivisionId });
            modelBuilder.Entity<LpdDivision>().HasAlternateKey(p => p.DivisionName).HasName("LpdDivisionNameUk");
            modelBuilder.Entity<LpdDivision>().HasKey(p => p.DivisionNameId);
            modelBuilder.Entity<LprDivision01>().HasOne(d => d.DivisionNameDict)
                .WithMany(m => m.DivisionRef01)
                .HasForeignKey(d => d.DivisionNameIdRef)
                .HasPrincipalKey(p => p.DivisionNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
            modelBuilder.Entity<LprDivision02>().HasOne(d => d.DivisionNameDict)
                .WithMany(m => m.DivisionRef02)
                .HasForeignKey(d => d.DivisionNameIdRef)
                .HasPrincipalKey(p => p.DivisionNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
        }

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

        }

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

        }

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

        }
    }
}

Regenerate LpdDivisionView and LpdDivisionViewPage

  • right click PhBk-folder of the LpPhBkViews-project and run ModelViews Wizard
  • select LpPhBkContext.csproj, LpPhbkDbContext on the second page
  • select LpPhBkEntity.csproj, LpdDivision on the third page
Click to show the picture

project structure

  • Note: The generated C# code will be the same as at modeling time. But the new Wizard's repo will be created for the LpPhbkDbContext.

Regenerate LprDivision01View and LprDivision01ViewPage

  • right click PhBk-folder of the LpPhBkViews-project and run ModelViews Wizard
  • select LpPhBkContext.csproj, LpPhbkDbContext on the second page
  • select LpPhBkEntity.csproj, LprDivision01 on the third page
    • repeat the steps described in the article Views for LprDivision01
      • Note: You do not require to configure foreign keys which are removed for the current entity's version.
Click to show the picture

project structure

  • Note: The generated C# code will be the same as at modeling time. But the new Wizard's repo will be updated for the LpPhbkDbContext.

Regenerate LprDivision02View and LprDivision02ViewPage

  • right click PhBk-folder of the LpPhBkViews-project and run ModelViews Wizard
  • select LpPhBkContext.csproj, LpPhbkDbContext on the second page
  • select LpPhBkEntity.csproj, LprDivision02 on the third page
    • repeat the steps described in the article Views for LprDivision02
      • Note: You do not require to configure foreign keys which are removed for the current entity's version.
Click to show the picture

project structure

  • Note: The generated C# code will be the same as at modeling time. But the new Wizard's repo will be updated for the LpPhbkDbContext.
⚠️ **GitHub.com Fallback** ⚠️