Open Closed

Extending Abp entities by adding navigation properties #295


User avatar
0
alkaabi created
  • ABP Framework version: v3.0.3
  • UI type: Angular
  • Tiered (MVC) or Identity Server Seperated (Angular): yes

Hi Abp Team,

I am trying to extend Abp OrganizationUnit by introducing extra properties. I have looked at the documentation on how to do that by using ObjectExtensionManager.Instance.Modules(). However, what I am not able to figure out is adding a property which is a foreign key to another entity/table.

Say for example I have an entity called "OrganizationUnitCategory" which has a column called Id of type Guid. I need a property named UnitCategoryId to be added to Abp OrganizationUnit. Adding UnitCategoryId is strightforward but defining the navigation property of Type "OrganizationUnitCategory​" is unclear for me.

You help is appreciated.

Thank you


4 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    There is an example to help you : https://github.com/abpframework/abp-samples/tree/master/DocumentationSamples/CustomApplicationModules.

  • User Avatar
    0
    alkaabi created

    I dont think this answers my questions unless I don't understand what is going on.

    I will show you what I have done and you can direct me If I am on the right track.

    I have created a UnitCategory class

    I have added the extra properties to OrganizationUnit as documented in the docs.abp.io

    and added the mappings

    and Defined the navigation properties

    and that is what I get in migration

    When I run the migrator project, this is what I get:

    It breaks in SeedAsync method.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    See https://github.com/abpframework/abp/issues/1414#issuecomment-508818941.

    If you still want add navigation properties, please do this:

    Entity:

    public class UnitCategory : Entity<Guid>
    {
        public UnitCategory(Guid id)
        {
            Id = id;
        }
    
        public List<OrganizationUnit> OrganizationUnits { get; set; }
    }
    

    DbContext:

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    
        /* Configure the shared tables (with included modules) here */
    
        builder.Entity<AppUser>(b =>
        {
            b.ToTable(AbpIdentityDbProperties.DbTablePrefix +
                      "Users"); //Sharing the same table "AbpUsers" with the IdentityUser
    
            b.ConfigureByConvention();
            b.ConfigureAbpUser();
    
            /* Configure mappings for your additional properties
             * Also see the qaEfCoreEntityExtensionMappings class
             */
        });
    
        // add this..
        builder.Entity<OrganizationUnit>(b =>
        {
            b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "OrganizationUnits");
    
            b.ConfigureByConvention();
    
            b.Property(ou => ou.Code).IsRequired().HasMaxLength(OrganizationUnitConsts.MaxCodeLength)
                .HasColumnName(nameof(OrganizationUnit.Code));
            b.Property(ou => ou.DisplayName).IsRequired().HasMaxLength(OrganizationUnitConsts.MaxDisplayNameLength)
                .HasColumnName(nameof(OrganizationUnit.DisplayName));
    
            b.HasMany<OrganizationUnit>().WithOne().HasForeignKey(ou => ou.ParentId);
            b.HasMany(ou => ou.Roles).WithOne().HasForeignKey(our => our.OrganizationUnitId).IsRequired();
    
            b.HasIndex(ou => ou.Code);
        });
    
        builder.Ignore<OrganizationUnitRole>();
    
        builder.Configureqa();
    }
    

    DbContextModelCreatingExtensions:

    builder.Entity<UnitCategory>(b =>
    {
        b.ToTable("UnitCategory");
        b.HasMany(x => x.OrganizationUnits).WithOne().HasForeignKey("UnitCategoryId");
        b.ConfigureByConvention();
    });
    

    EfCoreEntityExtensionMappings:

    ObjectExtensionManager.Instance
        .MapEfCoreProperty<OrganizationUnit, Guid?>(
            "UnitCategoryId",
            b => b.HasMaxLength(64)
        );
    

  • User Avatar
    0
    alkaabi created

    Thnak you, Works like a charm.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09