我们正在使用ASP.NET Zero / BoilerPlate,并且在扩展非抽象实体并使用IRepository访问数据时遇到了以下问题。
我已经将OrganizationUnit实体扩展到OrganisationUnit,以便可以添加一些其他属性,即:
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)
{
}
}
但是,当我们尝试使用IRepository时,没有任何结果,例如:
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;
}
}
我试图在GetOUForEmployee中执行的操作是找到OrganisationUnit,而不是OrganizationUnit。但是什么也没找到。使用.GetAll()会突出问题,因为此处的IRepository不会在扩展表上返回任何结果。
答案 0 :(得分:0)
您需要将新的DbSet添加到您的DbContext。并添加迁移。
public class AbpZeroTemplateDbContext : AbpZeroDbContext<Tenant, Role, User, AbpZeroTemplateDbContext>, IAbpPersistedGrantDbContext
{
//...
public virtual DbSet<OrganisationUnit> MyOrganizationUnits { get; set; }
//...
}
扩展OrganizationUnit的步骤
请参见https://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=11109&hilit=extend