Search code examples
aspnetboilerplate

Adding users list in Tenant.cs


I have started working on with the boilerplate template using Angular, .NET Framework and Module Zero.

I am using multi-tenancy and I'm trying to modify the Tenants view so that it lists users in every tenant so I tried adding public virtual ICollection<User> Users { get; set; } to the Tenant class and public virtual Tenant Tenant { get; set; } to the User class.

After migration and updating the database everything looks good in db. In GetTenants() the tenantManager returns tenants with the Users list being null and the same goes to users getting null as Tenant from userManager.

Is there any build in way or best practices doing this with boilerplate?


Solution

  • If you are using EntityFrameworkCore, you have to use eager-loading.

    // TenantManager.cs
    var tenants = TenantRepository.GetAllIncluding(t => t.Users);
    
    // UserManager.cs
    var users = AbpStore.UserRepository.GetAllIncluding(u => u.Tenant);
    

    Remember to configure inverse property:

    [InverseProperty("Tenant")]
    public virtual ICollection<User> Users { get; set; }
    

    You need to disable IMayHaveTenant filter to view cross-tenant entities:

    using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
    {
        var tenants = TenantRepository.GetAllIncluding(t => t.Users);
    }