Base solution for your next web application
Open Closed

Extending Organization Fails #8882


User avatar
0
darutter0508 created

I am trying to extend OrganizationUnit to add an integer value that would be populated from an enumeration. I found the post on extending the OrganizationUnit and followed it as best I could, but even before I get to the point of needing to display / retrieve the data from the input screen it fails to retrieve records from the database. I had entered data into the AbpOrganizationUnit table using the "out of the box" configuration prior to making the extended object. As soon as I create the extended object and put the reference in the DbContext.cs file, the call to GetAllListAsync() fails.

My extended OrganizationUnit class:

using Abp.Organizations;
using Sapphire.Enums;

namespace Sapphire.Organizations
{
    public class ExtendedOrganizationUnit : OrganizationUnit
    {
        public OrganizationTypes OrganizationTypeId { get; set; } = OrganizationTypes.Administration;
    }
}

in my DbContext.cs file:

public virtual DbSet<ExtendedOrganizationUnit> ExtendedOrganizationUnits { get; set; }

I ran the migration and the new column (OrganizationTypeId) is now in the table.

When I run the call:

var organizationUnits = await _organizationUnitRepository.GetAllListAsync();

it executes without any errors but returns no rows, even though prior to adding the entry in the DbContext.cs file, it would return my 8 existing rows.

Can you provide some guidance on how to resolve this?


2 Answer(s)
  • User Avatar
    0
    darutter0508 created

    I am using .Net Core with jQuery version 8.3.0

  • User Avatar
    0
    aaron created
    Support Team

    How to resolve this?

    Do a data patch:

    UPDATE SapphireDb.dbo.AbpOrganizationUnits
    SET Discriminator='ExtendedOrganizationUnit'
    WHERE Discriminator='';
    

    You can also do SET Discriminator='OrganizationUnit' if you do not want old OrganizationUnit entries to be recognised as ExtendedOrganizationUnit.

    Explanation

    Doing _organizationUnitRepository.GetAllListAsync() will only get those with the Discriminator value of OrganizationUnit and its subclasses, i.e. excluding ''.

    From https://docs.aspnetzero.com/en/aspnet-core-mvc/v8.2.0/Extending-Existing-Entities:

    Before applying changes to the database, we changed default migration code from:

    AddColumn("dbo.AbpEditions", "Price", c => c.Int());
    AddColumn("dbo.AbpEditions", "Discriminator", c => c.String(nullable: false, maxLength: 128));
    

    to:

    AddColumn("dbo.AbpEditions", "Price", c => c.Int(nullable: false, defaultValue: 0));
    AddColumn("dbo.AbpEditions", "Discriminator", c => c.String(nullable: false, maxLength: 128, defaultValue: "MyEdition"));
    

    It appears that you did not change the Discriminator column's defaultValue as described.