Base solution for your next web application
Open Closed

Read Host data from second Tenant database #2871


User avatar
0
ivanosw1 created

Hi, I've a table with IMayHaveTenantId created with some host data. In code I need to get all Host's data plus Tenant's data. This is a simple step: for host query inside a using (unitOfWorkManager.Current.SetTenantId(null)) and after this query for tenant. So far so good.

The problem is when I configure a new Tenant in a second database. When I query for host's data the query is executed against the second database and not where I guess, in the first created host database. This behaviour forces me to duplicate host data for every separated tenant database (and keep them in sync). There is a workaround in order to have only a single source of host's data ?

Thank you. Ivano


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

    Hi,

    This is not the expected behaviour. Can you share your code, dbContext and ABP version ?

    Thanks.

  • User Avatar
    0
    ivanosw1 created

    This is the code. A simple list of items shared between host and tenant. Tenant is on separated database. I would like to manage only one host list for every tenant either local or separate. Host and Tenant 1 on DB1 Tenant2 on DB2

    // This call is authenticated as Tenant2 and host items are retrived from host table in DB2.

    var dtos = new List<DossierStateTypeDto>();

            // Host 
            using (CurrentUnitOfWork.SetTenantId(null)) 
            {
                var hostItems = await dossierStateTypeRepository.GetAllListAsync();
                dtos.AddRange(hostItems.MapTo&lt;List&lt;DossierStateTypeDto&gt;>());
            }
    
            // Tenant
            var tenantItems = await dossierStateTypeRepository.GetAllListAsync();
            dtos.AddRange(tenantItems.MapTo&lt;List&lt;DossierStateTypeDto&gt;>());
    
            var result = new ListResultDto&lt;DossierStateTypeDto&gt;(dtos);
            return result;
    

    Thanks, Ivano

  • User Avatar
    0
    ivanosw1 created

    Hi, I haven't yet received response to my question, but after many days of real work on framework, I think that your answer This is not the expected behaviour. means a different thing that I supposed.

    Further test with Host and Tenant data, seems to confirm that every query against Host data is always executed on initial Host database and never against remote databases.

    Can you kindly confirm that ?

  • User Avatar
    0
    alirizaadiyahsi created

    Hi,

    Further test with Host and Tenant data, seems to confirm that every query against Host data is always executed on initial Host database and never against remote databases.

    Yes, this is true.

    According to code that you shared, if you login with a tenant, even if the tenant has a seperate database. Your query inside using statement should bring host data from initial host database because of your using statement.

    using (CurrentUnitOfWork.SetTenantId(null))
    

    Then, the code outside of your using statement should bring data from tenant's database even if it is a seperate database.