Base solution for your next web application
Open Closed

GetCurrentTenant method #10049


User avatar
0
tankods created

AspNet Zero 10.1 Angular - .Net Core

Hello!

Please explain why you will switch to the host in the method GetCurrentTenant of AppServiceBase class: protected virtual Task<Tenant> GetCurrentTenantAsync() { using (CurrentUnitOfWork.SetTenantId(null)) { return TenantManager.GetByIdAsync(AbpSession.GetTenantId()); } }

Best regards!


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @tankods

    Because, there is an active multi tenancy filter which filters entities which implement IMayHaveTenant or IMustHaveTenant interface according the value of current TenantId in AbpSession. If we don't switch host side, the query will return 0 records.

    You can read more here https://aspnetboilerplate.com/Pages/Documents/Data-Filters

  • User Avatar
    0
    tankods created

    Hi, thanks for your answer, but tenant entity don't impliment IMayHaveTenant or IMustHaveTenant interface, and if i correctly understood filter, CurrentUnitOfWork.SetTenantId(null) make what we select only entities of host.

    Best regards.

  • User Avatar
    1
    ismcagdas created
    Support Team

    Hi @tankods

    Correct. But, for Tenant entity, it is stored in Host database. AspNet Zero allows Tenants to use separate databases. So, when this code is accessed from a Tenant context, using CurrentUnitOfWork.SetTenantId(null) will allow app to access Host database. If we don't use it, there will be a problem when using this code from a Tenant context with a separate database.

  • User Avatar
    0
    tankods created

    Thank you, forgot about separate databases feature.