028 Modify DBContext for the entity with foreign key (Division) - chempkovsky/CS82ANGULAR GitHub Wiki
- Run DBContext Wizard
- First page of DBContext Wizard
- Second page of DBContext Wizard
- Sixth page of DBContext Wizard
- Primary Key page of DBContext Wizard
- Unique Key page of DBContext Wizard
- Second page of DBContext Wizard second time
- Foreign Keys page of DBContext Wizard
- Create or Modify Foreign Keys page of DBContext Wizard
- 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 PhbkEnterprise-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<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

- 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
- 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<PhbkDivision>().HasKey(p => p.DivisionId);
...
}Click Next-button to navigate to 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

- To modify Foreign Key Setting click
Next-button
Click to show the picture

- The Wizard shown only one foreign key which is 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

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
- it shows
- click
- you will be back on the third page
Click to show the picture

- close the Wizard