068 Entity with two ForeignKeys : Phone - chempkovsky/CS82ANGULAR GitHub Wiki

According to the rules defined in the article 006 Development Process Cycle

We can continue with any entity from the list: Phone Let's continue with PhbkPhone

  • Step #1:
    • Run Visual Studio and Open PhonebookSolution- solution
      • Right Click PhBk of the PhBkEntity-project
      • Select Add/Class-menu item
      • In the dialog enter the name for the class
      • PhbkPhone
      • Click Add-button
  • Step #2:
    • Open PhbkPhone.cs-file and modify the body of the class as it is shown on the slide
Click to show the PhbkPhone.cs file
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PhBkEntity.PhBk
{
    public class PhbkPhone
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Display(Description = "Row id", Name = "Phone Id", Prompt = "Enter Phone Id", ShortName = "Phone Id")]
        [Required]
        public int PhoneId { get; set; }

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

        [Display(Description = "Row id", Name = "Phone Type Id", Prompt = "Enter Phone Type Id", ShortName = "Phone Type Id")]
        [Required]
        public int PhoneTypeIdRef { get; set; }

        public PhbkPhoneType PhoneType { get; set; } = null!;


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

        public PhbkEmployee Employee { get; set; } = null!;

    }
}
  • PhbkPhone refers to PhbkEmployee as the master entity.
  • to complete description of the Foreign Key PhbkEmployee needs to be modified as it is shown below:
Click to show the PhbkEmployee.cs file
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PhBkEntity.PhBk
{
    public class PhbkEmployee
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Display(Description = "Row id", Name = "Id of the Employee", Prompt = "Enter Employee  Id", ShortName = "Employee Id")]
        [Required]
        public int EmployeeId { get; set; }

        [Display(Description = "First Name of the Employee", Name = "Employee First Name", Prompt = "Enter Employee First Name", ShortName = "First Name")]
        [StringLength(25, MinimumLength = 3, ErrorMessage = "Invalid")]
        [Required]
        public string EmpFirstName { get; set; } = null!;

        [Display(Description = "Last Name of the Employee", Name = "Employee Last Name", Prompt = "Enter Employee Last Name", ShortName = "Last Name")]
        [StringLength(40, MinimumLength = 3, ErrorMessage = "Invalid")]
        [Required]
        public string EmpLastName { get; set; } = null!;

        [Display(Description = "Row id", Name = "Employee Second Name", Prompt = "Enter Employee Second Name", ShortName = "Second Name")]
        [StringLength(25, ErrorMessage = "Invalid")]
        public string? EmpSecondName { get; set; }

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

        public PhbkDivision Division { get; set; } = null!;

        public List<PhbkPhone> Phones { get; set; } = null!;


#if (!NOTMODELING)
        public List<LprEmployee01> EmployeeRefs01 { get; set; } = null!;
        public List<LprEmployee02> EmployeeRefs02 { get; set; } = null!;
#endif

    }
}
  • PhbkPhone refers to PhbkPhoneType as the master entity.
  • to complete description of the Foreign Key PhbkPhoneType needs to be modified as it is shown below:
Click to show the PhbkPhoneType.cs file
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PhBkEntity.PhBk
{
    public class PhbkPhoneType
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Description = "Row id", Name = "Phone Type Id", Prompt = "Enter Phone Type Id", ShortName = "Phone Type Id")]
        [Required]
        public int PhoneTypeId { get; set; }

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

        [Display(Description = "Description of the Phone Type", Name = "Phone Type Description", Prompt = "Enter Phone Type Description", ShortName = "Phone Type Description")]
        [StringLength(250, ErrorMessage = "Invalid")]
        public string? PhoneTypeDesc { get; set; }

        public List<PhbkPhone> Phones { get; set; } = null!;

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