Open Closed

Console Application with DataFilter #292


User avatar
0
ninomartini created

ABP Framework version: v2.9 UI type: Angular Tiered (MVC) or Identity Server Seperated (Angular): no

I have a console application that needs to modify records accross multiple tenants. The console application uses the host admin credentials. I tested out the code below without success:

using (_dataFilter.Disable<IMultiTenant>())
{
    var unit = await _unitAppService.GetAsync(Guid.Parse("ED9D4AC2-BC3F-1FA4-71D6-39F5F9048F20"));
    Console.WriteLine(unit.UniqueIdentifier);
}

I get the following error:

**Volo.Abp.Http.Client.AbpRemoteCallException: 'There is no entity Unit with id = ed9d4ac2-bc3f-1fa4-71d6-39f5f9048f20!'**

If I change the console application to the tenant's credentials and use ICurrentTenant to change the tenant I can find the record.

Any thoughts on what I am doing wrong?


6 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Because the data is isolated, The host cannot read the tenant's data. If you want read all tenant's data. you can get all tenants and loop get the data. like this:

    var tenants = await _tenantRepository.GetListAsync();
    
    foreach (var tenant in tenants)
    {
        using (CurrentTenant.Change(tenant.Id))
        {
              var unit = await _unitAppService.GetAsync(Guid.Parse("ED9D4AC2-BC3F-1FA4-71D6-39F5F9048F20"));
        }
    }
    
  • User Avatar
    0
    ninomartini created

    Thank you for your quick response. According to one of your earlier responses, I thought that _dataFilter.Disable<IMultiTenant>() would achive my goal.

    [ How to inject a repository to supply all the tenantfull and tenantless records of a database table? #115](https://support.abp.io/QA/Questions/115/How-to-inject-a-repository-to-supply-all-the-tenantfull-and-tenantless-records-of-a-database-table)

    When and how is _dataFilter.Disable<IMultiTenant>() applied?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi.

    DataFilter only use shared database. If you use a tenant-separated database, DataFilter is not work.

  • User Avatar
    0
    ninomartini created

    I am using a shared database solution. Why did DataFilter not work?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Volo.Abp.Http.Client.AbpRemoteCallException

    I see you are using remote service, You need to disable the multi-tenant filter in the service implementation. like this:

    public async Task<Unit> GetAsync(Guid id, bool ignoreTenant = false)
    {
           if(ignoreTenant){
                using (_dataFilter.Disable<IMultiTenant>())
                {
                    //query...
                }
           }
           else{
                   // query...
           }
    }
    
  • User Avatar
    0
    ninomartini created

    Understood, thank you.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11