028 Modify DBContext for the entity with foreign key (Division) - chempkovsky/CS82ANGULAR GitHub Wiki

Using DBContext Wizard is absolutely necessary

Steps required to accomplish the task

Run DBContext Wizard
  • repeat the steps described in the article
First page of DBContext Wizard
  • repeat the steps described in the article
Second page of DBContext Wizard
  • repeat the steps described in the article
Sixth page of DBContext Wizard

On the Sixth page of the Wizard choose PhBkEntity.csproj and PhbkEnterprise-class and click Add Required property to Db-context-button

Click to show the picture

project structure

It will add the following lines of code to the PhbkDbContext.cs file.

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

In addition the content of the second page of the Wizard has changed. Now it shows the message about the primary Key of the entity. Click Modify-button. (It will show Primary Key page of DBContext Wizard.)

Click to show the picture

project structure

Primary Key page of DBContext Wizard
  • On the Primary Key page
    • choose DivisionId-property to be used as a primary key,
    • choose HasKey.Net.Core.cs.t4-script to generate the code
    • click Create(modify)-button
Click to show the picture

project structure

It modifies OnModelCreating-method of the DbContext class as shown below:

Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             ...
             modelBuilder.Entity<PhbkDivision>().HasKey(p => p.DivisionId);
             ...
        }

Click Next-button to navigate to Unique Key page of DBContext Wizard

Unique Key page of DBContext Wizard

We do not require additional unique keys for the PhbkDivision-entity, so click Next-button on this page. It will navigate to the second page of the wizard.

Click to show the picture

project structure

Second page of DBContext Wizard second time
  • To modify Foreign Key Setting click Next-button
Click to show the picture

project structure

Foreign Keys page of DBContext Wizard
  • The Wizard shown only one foreign key which is not completely defined
    • click first row in the panel
    • click Next button
Click to show the picture

project structure

Create or Modify Foreign Keys page of DBContext Wizard
  • On this page of the Wizard
    • select EntrprsIdRef-property of the PhbkDivision-entity
    • select One-to-Many-type of the foreign key
    • select OneToCollection.Net.Core.cs.t4-script
    • click Create(Modify)-button
Click to show the picture

project structure

It will modify OnModelCreating-method

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ...
            modelBuilder.Entity<PhbkDivision>().HasOne(d => d.Enterprise)
                .WithMany(m => m.Divisions)
                .HasForeignKey(d => d.EntrprsIdRef)
                .HasPrincipalKey(p => p.EntrprsId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
            ...
        }
  • click Next-button
    • you will be back on the third page
      • click Next-button
        • it shows Foreign Keys-page of the Wizard
Click to show the picture

project structure

  • close the Wizard
⚠️ **GitHub.com Fallback** ⚠️