035 Modify DBContext for the Modeling Lookup Resource of PhbkDivision - chempkovsky/CS82ANGULAR GitHub Wiki

Using DBContext Wizard is absolutely necessary

The list of the Modeling Tables

  • We need to modify DBContext for the following list of the modeling tables
    • LpdDivision
    • LprDivision01
    • LprDivision02
  • Since, these tables for modeling only all the modifications must be inside #if (!NOTMODELING) ... #endif-operator.

Reset Directory Build props file

Before we start, open Directory.Build.props-file and modify it as follows:

<Project>
    <PropertyGroup>
        <DefineConstants>MODELING</DefineConstants>
    </PropertyGroup>
</Project>
  • save the Directory.Build.props-file
  • rebuild PhBkContext-project

Steps required to accomplish the task

Modification related to LpdDivision

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

On the Sixth page of the Wizard choose PhBkEntity.csproj and LpdDivision-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<LpdDivision> LpdDivisionDbSet {
            get => Set<LpdDivision>();
            
        }
  • 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

Note 1 for LpdDivision
  • According to the requirements 033
    • after closing the Wizard you should put generated code inside #if (!NOTMODELING) ... #endif-operator. So the code will be as follows:
#if (!NOTMODELING)
        public DbSet<LpdDivision> LpdDivisionDbSet {
            get => Set<LpdDivision>();
            
        }
#endif
Primary Key page of DBContext Wizard for LpdDivision
  • On the Primary Key page
    • choose DivisionNameId-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<LpdDivision>().HasKey(p => p.DivisionNameId);
             ...
        }

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

Note 2 for LpdDivision
  • According to the requirements 033
    • after closing the Wizard you should put generated code inside #if (!NOTMODELING) ... #endif-operator. So the code will be as follows:
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             ...
#if (!NOTMODELING)
             modelBuilder.Entity<LpdDivision>().HasKey(p => p.DivisionNameId);
#endif
             ...
        }
Unique Key page of DBContext Wizard for LpdDivision
  • On the Unique Key page
    • enter LpdDivisionNameUk as a unique key name. The Unique key name is a required property. It is used by the generator scripts.
    • choose DivisionName-property to be used as a Unique key,
    • choose HasAlternateKey.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<LpdDivision>().HasAlternateKey(p => p.DivisionName).HasName("LpdDivisionNameUk");
             ...
        }
Note 3 for LpdDivision
  • According to the requirements 033
    • after closing the Wizard you should put generated code inside #if (!NOTMODELING) ... #endif-operator. So the code will be as follows:
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             ...
#if (!NOTMODELING)
             modelBuilder.Entity<LpdDivision>().HasAlternateKey(p => p.DivisionName).HasName("LpdDivisionNameUk");
#endif
             ...
        }

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

Second page of DBContext Wizard for LpdDivision second time

Click Cancel-button to close DBContext Wizard

Click to show the picture

project structure

Modification related to LprDivision01

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

On the Sixth page of the Wizard choose PhBkEntity.csproj and LprDivision01-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<LprDivision01> LprDivision01DbSet {
            get => Set<LprDivision01>();
        }
  • 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.)
Note 1 for LprDivision01
  • According to the requirements 033
    • after closing the Wizard you should put generated code inside #if (!NOTMODELING) ... #endif-operator. So the code will be as follows:
#if (!NOTMODELING)
        public DbSet<LprDivision01> LprDivision01DbSet {
            get => Set<LprDivision01>();
        }
#endif
Primary Key page of DBContext Wizard for LprDivision01
  • On the Primary Key page
    • choose DivisionNameIdRef and DivisionId-properties to be used as a primary key,
      • the order is important: DivisionNameIdRef must be the first
    • 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<LprDivision01>().HasKey(p => new { p.DivisionNameIdRef, p.DivisionId });
             ...
        }

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

Note 2 for LprDivision01
  • According to the requirements 033
    • after closing the Wizard you should put generated code inside #if (!NOTMODELING) ... #endif-operator. So the code will be as follows:
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             ...
#if (!NOTMODELING)
             modelBuilder.Entity<LprDivision01>().HasKey(p => new { p.DivisionNameIdRef, p.DivisionId });
#endif
             ...
        }
Unique Key page of DBContext Wizard for LprDivision01

LprDivision01 will not have Unique Keys. So, click Next-button to navigate to Second page of DBContext Wizard

Click to show the picture

project structure

Second page of DBContext Wizard for LprDivision01 second time

After defining the primary key you are on the second page of the Wizard. Click Next-button. The Wizard shows the page with a list of foreign keys.

Click to show the picture

project structure

Foreign Keys page of DBContext Wizard for LprDivision01
  • The Wizard shown only two foreign keys which are 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 for LprDivision01 first time
  • 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 modifies `OnModelCreating`-method of the DbContext class as shown below:
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ...  
            modelBuilder.Entity<LprDivision01>().HasOne(d => d.Division)
                .WithMany(m => m.DivisionRefs01)
                .HasForeignKey(d => d.DivisionId)
                .HasPrincipalKey(p => p.DivisionId)
                .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
Note 3 for LprDivision01
  • According to the requirements 033
    • after closing the Wizard you should put generated code inside #if (!NOTMODELING) ... #endif-operator. So the code will be as follows:
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             ...
#if (!NOTMODELING)
            modelBuilder.Entity<LprDivision01>().HasOne(d => d.Division)
                .WithMany(m => m.DivisionRefs01)
                .HasForeignKey(d => d.DivisionId)
                .HasPrincipalKey(p => p.DivisionId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
#endif
             ...
        }
Foreign Keys page of DBContext Wizard for LprDivision01 second time
  • The Wizard shown only two foreign keys with one which is not completely defined
    • click second row in the panel
    • click Next button
Click to show the picture

project structure

Create or Modify Foreign Keys page of DBContext Wizard for LprDivision01 second time
  • On this page of the Wizard
    • select DivisionNameIdRef-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 modifies `OnModelCreating`-method of the DbContext class as shown below:
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ...  
            modelBuilder.Entity<LprDivision01>().HasOne(d => d.DivisionNameDict)
                .WithMany(m => m.DivisionRef01)
                .HasForeignKey(d => d.DivisionNameIdRef)
                .HasPrincipalKey(p => p.DivisionNameId)
                .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
Note 4 for LprDivision01
  • According to the requirements 033
    • after closing the Wizard you should put generated code inside #if (!NOTMODELING) ... #endif-operator. So the code will be as follows:
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ...  
#if (!NOTMODELING)
            modelBuilder.Entity<LprDivision01>().HasOne(d => d.DivisionNameDict)
                .WithMany(m => m.DivisionRef01)
                .HasForeignKey(d => d.DivisionNameIdRef)
                .HasPrincipalKey(p => p.DivisionNameId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
#endif
            ... 
        }
Foreign Keys page of DBContext Wizard for LprDivision01 third time
Click to show the picture

project structure

  • close the Wizard

Modification related to LprDivision02

DbSet property for LprDivision02
  • With actions similar to those described above we created LprDivision02DbSet-property
Click to show the code
#if (!NOTMODELING)
        public DbSet<LprDivision02> LprDivision02DbSet {
            get => Set<LprDivision02>();
            
        }
#endif
Primary key for LprDivision02
  • With actions similar to those described above we modified OnModelCreating-method by adding primary key definition
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             ...
#if (!NOTMODELING)
             modelBuilder.Entity<LprDivision02>().HasKey(p => new { p.EntrprsIdRef, p.DivisionNameIdRef, p.DivisionId });
#endif
             ...
        }
Unique Key for LprDivision02

LprDivision02 will not have Unique Keys.

Foreign Keys for LprDivision02
  • With actions similar to those described above we modified OnModelCreating-method by adding foreign keys definition
Click to show the code
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             ...
#if (!NOTMODELING)
            modelBuilder.Entity<LprDivision02>().HasOne(d => d.Division)
                .WithMany(m => m.DivisionRefs02)
                .HasForeignKey(d => d.DivisionId)
                .HasPrincipalKey(p => p.DivisionId)
                .IsRequired(true)
                .OnDelete(DeleteBehavior.NoAction);
            modelBuilder.Entity<LprDivision02>().HasOne(d => d.Enterprise)
                .WithMany(m => m.DivisionRefs02)
                .HasForeignKey(d => d.EntrprsIdRef)
                .HasPrincipalKey(p => p.EntrprsId)
                .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);
#endif
             ...
        }

Reset Directory Build props file again

At the end, open Directory.Build.props-file and modify it as follows:

<Project>
    <PropertyGroup>
        <DefineConstants>NOTMODELING</DefineConstants>
    </PropertyGroup>
</Project>
  • save the Directory.Build.props-file
  • rebuild PhBkContext-project
  • with Visual Studion open the PhbkDbContext.cs-file
    • here is the result: the ignored code is grayed out
Click to show the picture

project structure

  • Note:
    • Using #if (MODELING) ... #endif-operator (without negation sign) does not work in our test environment. The Wizards could not correct parse the code without negation sign. We do use Roslyn-parser for code analysis of the only the file fragment. The only OnModelCreating-method is parsered. EnvDTE functions are used to detect the boundary (the position of the first and the last symbol) of the OnModelCreating-method. (We tried to explain why).
    • Using #if (!NOTMODELING) ... #endif-operator does work in our test environment.
⚠️ **GitHub.com Fallback** ⚠️