035 Modify DBContext for the Modeling Lookup Resource of PhbkDivision - chempkovsky/CS82ANGULAR GitHub Wiki
-
Modification related to LpdDivision
- Run DBContext Wizard for LpdDivision
- First page of DBContext Wizard for LpdDivision
- Second page of DBContext Wizard for LpdDivision
- Sixth page of DBContext Wizard for LpdDivision
- Primary Key page of DBContext Wizard for LpdDivision
- Unique Key page of DBContext Wizard for LpdDivision
- Second page of DBContext Wizard for LpdDivision second time
-
Modification related to LprDivision01
- Run DBContext Wizard for LprDivision01
- First page of DBContext Wizard for LprDivision01
- Second page of DBContext Wizard for LprDivision01
- Sixth page of DBContext Wizard for LprDivision01
- Primary Key page of DBContext Wizard for LprDivision01
- Unique Key page of DBContext Wizard for LprDivision01
- Second page of DBContext Wizard for LprDivision01 second time
- Foreign Keys page of DBContext Wizard for LprDivision01
- Create or Modify Foreign Keys page of DBContext Wizard for LprDivision01 first time
- Foreign Keys page of DBContext Wizard for LprDivision01 second time
- Create or Modify Foreign Keys page of DBContext Wizard for LprDivision01 second time
- Foreign Keys page of DBContext Wizard for LprDivision01 third time
- 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.
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
- repeat the steps described in the article
- repeat the steps described in the article
- repeat the steps described in the article
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

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

- 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:
- after closing the Wizard you should put generated code inside
#if (!NOTMODELING)
public DbSet<LpdDivision> LpdDivisionDbSet {
get => Set<LpdDivision>();
}
#endif- 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
- choose
Click to show the picture

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
- 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:
- after closing the Wizard you should put generated code inside
Click to show the code
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
...
#if (!NOTMODELING)
modelBuilder.Entity<LpdDivision>().HasKey(p => p.DivisionNameId);
#endif
...
}- On the Unique Key page
- enter
LpdDivisionNameUkas 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
- enter
Click to show the picture

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");
...
}- 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:
- after closing the Wizard you should put generated code inside
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
Click Cancel-button to close DBContext Wizard
Click to show the picture

- repeat the steps described in the article
- repeat the steps described in the article
- repeat the steps described in the article
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

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.)
- 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:
- after closing the Wizard you should put generated code inside
#if (!NOTMODELING)
public DbSet<LprDivision01> LprDivision01DbSet {
get => Set<LprDivision01>();
}
#endif- On the Primary Key page
- choose
DivisionNameIdRefandDivisionId-properties to be used as a primary key,- the order is important:
DivisionNameIdRefmust be the first
- the order is important:
- choose
HasKey.Net.Core.cs.t4-script to generate the code - click
Create(modify)-button
- choose
Click to show the picture

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
- 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:
- after closing the Wizard you should put generated code inside
Click to show the code
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
...
#if (!NOTMODELING)
modelBuilder.Entity<LprDivision01>().HasKey(p => new { p.DivisionNameIdRef, p.DivisionId });
#endif
...
}LprDivision01 will not have Unique Keys. So, click Next-button to navigate to Second page of DBContext Wizard
Click to show the picture

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

- The Wizard shown only two foreign keys which are not completely defined
- click first row in the panel
- click
Nextbutton
Click to show the picture

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

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
- it shows
- click
- you will be back on the third page
- 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:
- after closing the Wizard you should put generated code inside
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
...
}- The Wizard shown only two foreign keys with one which is not completely defined
- click second row in the panel
- click
Nextbutton
Click to show the picture

- On this page of the Wizard
- select
DivisionNameIdRef-property of thePhbkDivision-entity - select
One-to-Many-type of the foreign key - select
OneToCollection.Net.Core.cs.t4-script - click
Create(Modify)-button
- select
Click to show the picture

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
- it shows
- click
- you will be back on the third page
- 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:
- after closing the Wizard you should put generated code inside
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
...
}Click to show the picture

- close the Wizard
- 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- 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
...
}LprDivision02 will not have Unique Keys.
- 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
...
}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

-
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 onlyOnModelCreating-method is parsered. EnvDTE functions are used to detect the boundary (the position of the first and the last symbol) of theOnModelCreating-method. (We tried to explain why). - Using
#if (!NOTMODELING) ... #endif-operator does work in our test environment.
- Using