403 Modify DBContext for the first entity (PhoneType) - chempkovsky/CS82ANGULAR GitHub Wiki

Using DBContext Wizard is absolutely necessary

The DBContext Wizard is a tool that creates or modifies an existing DBContext file. There are a lot of such tools. And the question is, "why was it created."

  • The answer is as follows:
    • Other wizards (ModelViews, WebApiServices) parse the DBContext file and Entities files for their own needs. But the task of creating full-feature parser is a time consuming. On the other hand, only a few fluent api constructs are used by ModelViews, WebApiServices wizards. So, instead of creating a fully functional parser, I created a small code generator for a very short list of constructs. According to (https://www.entityframeworktutorial.net/efcore/configure-one-to-many-relationship-using-fluent-api-in-ef-core.aspx) some fluent api designs can be done in more than one way. To make sure that the ModelViews and WebApiServices wizards parse the fluent api structures correctly, a DBContext Wizard was developed. So, using DBContext Wizard is absolutely necessary. It generates fluent api constructs in a way that ModelViews and WebApiServices wizards parse correctly.

Steps required to accomplish the task

Run DBContext Wizard
  • Right Click EntityFrameworkCore-folder of the “rupbes.firstapp.EntityFrameworkCore.csproj”-project and Select “DBContext Wizard” menu item to open the Wizard dialog
Click to show the picture

project structure

First page of DBContext Wizard

The first page of the wizard displays information about the target project, namespace, and folder. Click Next-button

Click to show the picture

project structure

Second page of DBContext Wizard

On the second page of the wizard, select rupbes.firstapp.EntityFrameworkCore.csproj and the firstappDbContext class using the drop-down lists. Click Next-button

Click to show the picture

project structure

Third page of DBContext Wizard

On the Third page of the Wizard choose rupbes.firstapp.Domain.csproj and PhbkPhoneType-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<PhbkPhoneType> PhbkPhoneTypeDbSet
    {
        get => Set<PhbkPhoneType>();
    }

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 Id-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 builder)
        {
...
	    builder.Entity<PhbkPhoneType>().HasKey(p => p.Id);
...
        }

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 PhbkPhoneType-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

Click Cancel-button to close DBContext Wizard

Click to show the picture

project structure

CreatingExtensions class

In the EntityFrameworkCore-folder of the rupbes.firstapp.EntityFrameworkCore.csproj-project add a firstappDbContextCreatingExtensions class

Click to show the code
using Microsoft.EntityFrameworkCore;
using rupbes.firstapp.Phbk;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace rupbes.firstapp.EntityFrameworkCore
{
    public static class firstappDbContextCreatingExtensions
    {
        public static void ConfigurefirstappDbContext(
            this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            builder.Entity<PhbkPhoneType>(b =>
            {
                b.ToTable(firstappConsts.DbTablePrefix + "PhbkPhoneTypes", firstappConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
                                           //...
                builder.Entity<PhbkPhoneType>().HasKey(p => p.Id);
            });
        }

    }
}
Modify OnModelCreating

Modify OnModelCreating-method of the firstappDbContext-class as follows

Click to show the code
    protected override void OnModelCreating(ModelBuilder builder)
    {
#if (!NOTMODELING)
        builder.Entity<PhbkPhoneType>().HasKey(p => p.Id);
#endif

        base.OnModelCreating(builder);
        /* Include modules to your migration db context */
        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureFeatureManagement();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureTenantManagement();
        builder.ConfigureBlobStoring();
        /* Configure your own tables/entities inside here */
        //builder.Entity<YourEntity>(b =>
        //{
        //    b.ToTable(firstappConsts.DbTablePrefix + "YourEntities", firstappConsts.DbSchema);
        //    b.ConfigureByConvention(); //auto configure for the base class props
        //    //...
        //});

        builder.ConfigurefirstappDbContext();
    }
So we repaced the code
	builder.Entity<PhbkPhoneType>().HasKey(p => p.Id);

with the code

	builder.ConfigurefirstappDbContext();
NOTMODELING

Make sure our Directory.Build.props file is as follows:

<Project>
    <PropertyGroup>
        <DefineConstants>NOTMODELING</DefineConstants>
    </PropertyGroup>
</Project>
db migrations

Run Command Prompt and make E:\development\rupbes.firstapp\src\rupbes.firstapp.EntityFrameworkCore\ folder active. In the folder run the command:

dotnet ef migrations add Added_PhbkPhoneType
⚠️ **GitHub.com Fallback** ⚠️