Open Closed

Multi-Tenant shared data. #10425


User avatar
0
elhajjh2 created

Hi,

I have data that is common to all tenants, so each tenant needs to read its tenant data + the common data that should be shared to all tenants. How to apply this using abp?

We have this case in few tables in the system (not all tables have this scenario)

A more complex case is where some data is shared across some tenants (not all tenants)

Thank you.


2 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • Option A (Recommended): Model “common to all tenants” as Host data
      1. Create the shared entities/tables without IMultiTenant (no TenantId column). These are naturally readable by everyone because they are not tenant-filtered.

      2. For tables that need both tenant-specific rows and global rows, implement IMultiTenant and store shared rows with TenantId = null.

      3. When querying from the tenant side, explicitly include both:

        • rows with TenantId == CurrentTenantId
        • rows with TenantId == null (global/shared)

        Example (repository or EF Core query):

    var tenantId = CurrentTenant.Id;
    
         var items = await _repo.GetQueryableAsync();
         var result = await items
             .Where(x => x.TenantId == tenantId || x.TenantId == null)
             .ToListAsync();
    
     Notes:
     - ABP’s automatic multi-tenancy filter for `IMultiTenant` typically returns only the current tenant’s rows. So for this “tenant + global” scenario, you should **control the predicate yourself** (as above), or use a separate read model/service dedicated to this query pattern.
    
    • Option B (More complex): “Shared across some tenants (not all)”
      • This requirement violates strict multi-tenancy isolation (ABP team explicitly calls out that if tenants must see other tenants’ data, it’s not a pure multi-tenant model).

      • Practical approach in ABP is to model “sharing” explicitly, e.g.:

        1. Keep the record as a host/global record (no TenantId or TenantId = null)
        2. Add a join table like EntityTenantShare(EntityId, TenantId) that defines which tenants can see it
        3. Query with a join / subquery for the current tenant.

        Conceptual example:

    var tenantId = CurrentTenant.Id;
    
         var query = from e in await _entityRepo.GetQueryableAsync()
                     join s in await _shareRepo.GetQueryableAsync() on e.Id equals s.EntityId into shares
                     from share in shares.DefaultIfEmpty()
                     where e.TenantId == tenantId
                           || (e.TenantId == null && share.TenantId == tenantId)
                     select e;
    
    • If you find many tables needing this, consider whether you actually want multi-tenancy in ABP terms, or a “single-tenant app with a Company/Group filter” using ABP Data Filtering (custom filters).

    • When you need “not all tables”

      • ABP already supports this naturally: only entities implementing IMultiTenant are tenant-filtered. Others are shared.

    Links (verified):

    • https://abp.io/docs/latest/framework/architecture/multi-tenancy
    • https://abp.io/docs/latest/framework/infrastructure/data-filtering
    • https://abp.io/community/articles/hpwn44r3
    • https://abp.io/community/articles/51nvl4u9

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can create a multi-tenant entity. By default, each tenant can only see their own data. If you need to access other tenants' data, you can disable the multi-tenant filter.

    https://abp.io/docs/latest/framework/infrastructure/data-filtering?_redirected=B8ABF606AA1BDF5C629883DF1061649A#idatafilter-service-enabledisable-data-filters

    Thanks.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.2.0-preview. Updated on February 17, 2026, 09:10
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.