简体   繁体   中英

Extending an Entity and using IRepository

We're using ASP.NET Zero/BoilerPlate and have run into the following problem when extending a non-abstract entity and accessing the data using an IRepository.

I've extended the OrganizationUnit entity to OrganisationUnit so some additional properties can be added, ie:

public class OrganisationUnit : OrganizationUnit
    {
        public virtual bool IsCompany { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="OrganisationUnit"/> class.
        /// </summary>
        public OrganisationUnit()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OrganisationUnit"/> class.
        /// </summary>
        /// <param name="tenantId">Tenant's Id or null for host.</param>
        /// <param name="displayName">Display name.</param>
        /// <param name="parentId">Parent's Id or null if OU is a root.</param>
        public OrganisationUnit(int? tenantId, string displayName, long? parentId = null) : base(tenantId, displayName, parentId)
        {

        }
    }

However when we try to use an IRepository, there are no results, so for example:

public class OrganisationUnitManager : IDomainService
{
    private readonly IRepository<OrganisationUnit, long> _organisationUnitRepository;
    private readonly IRepository<Abp.Organizations.OrganizationUnit, long> _organizationUnitRepository;

    public OrganisationUnitManager(IRepository<OrganisationUnit, long> organisationUnitRepository, IRepository<Abp.Organizations.OrganizationUnit, long> organizationUnitRepository)
    {
        _organisationUnitRepository = organisationUnitRepository;
        _organizationUnitRepository = organizationUnitRepository;
    }

    public async Task<Abp.Organizations.OrganizationUnit> GetOUForEmployee(Employee.Employee employee)
    {
        var orgUnit = _organisationUnitRepository.FirstOrDefault(ou => ou.Id == employee.OrganizationUnitId);
        var orgS = _organisationUnitRepository.GetAll(); // Returns 0 Results - with the extended columns
        var orgZ = _organizationUnitRepository.GetAll(); // Returns All Records in AbpOrganizationUnits without the extended columns.
        return orgUnit;
    }
}

What Im trying to do in GetOUForEmployee, is to find the OrganisationUnit, not the OrganizationUnit. However nothing is found. Using .GetAll() highlights the problem in that the IRepository here is not returning any results on the extended table.

You need to add the new DbSet to your DbContext. And add the migration.

public class AbpZeroTemplateDbContext : AbpZeroDbContext<Tenant, Role, User, AbpZeroTemplateDbContext>, IAbpPersistedGrantDbContext
{
     //...

     public virtual DbSet<OrganisationUnit> MyOrganizationUnits { get; set; }

     //...
}

Steps to extend OrganizationUnit

  • Create a new class like ExtendedOrganizationUnit that inherits from OrganizationUnit
  • Add the new DbSet for ExtendedOrganizationUnit to the DbContext.
  • Add the added a migration
  • Update the database (saw the change successfully)

See https://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=11109&hilit=extend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM