Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How can I impliment multiple Tenant Types, each with their own displays and db schema? #10646


User avatar
0
4Matrix created

Prerequisites

Using Asp.net Core + Angular Solution v10.5

Question

For our application, we have 2 different tyes of Tenant, lets call the Company and Client.

  • The Client tenant will only store data about itself and will have its own set of displays. It pulls lookup data from the host database but also has its own database to store its data.
  • The Company tenant will pull data from multiple linked clients but will have its own set of data tables that are different from the client. It will also have a completly different set of displays that they see but they can also look a the "Client" screens on a per client basis.

If I want to have different tenant types, what is the best way to have different schemas for each tenant type and how can I link to the host database for the lookup data without just switching the tenant id in the dbcontext to look at 2 totally seperate databases?


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

    Hi @4Matrix

    First of all, you can add a Type field to Tenant as explained here.

    • for the client, if client wants to access data from Host, you can swithc to host context as explained here. Or you can define the data as non-multi tenant so all tenants can access to this data without switching the context.

    • for company to access data from multiple clients, you can store a new table to keep which customer can access to which client data (if necessary). Then, you can add a custom filter as explianed here. In this filter, you can filter a specific table with customer's allowed client list. For example let's say that clients can have a Product entity. You can filter it as shown below;

    public class Product : IMustHaveClient{
        public int ClientId {get;set;}
    }
    

    In your custom filter you can get the List of ClientIds which customer is allowed to view and filter it ;

    Expression<Func<TEntity, bool>> mustHaveClientFilter = e => ListOfAllowedClients.Contains(((IMustHaveClient)e).OrganizationUnitId );
    

    Another approach would be disabling the tenany filter and query the data when necessary and filter manually.

  • User Avatar
    0
    4Matrix created

    Thanks @ismcagdas that is useful. Is there a way to make different tables only appear in certain databases (Based on tenant type). For example, we have a CommonEthnicity table which stores data to be shared amongst all tenants. When we create a new tenant, it automatically creates a blank table called CommonEthnicity in the tenant database which is not then populated. Can we stop it making the tables at all? That way we would not need to add the "Common" prefix to those tables too.

    Ideally we want to be able to "Join" across the two contexts, e.g. Tenant and Host so that we can get a list of "Users" and their Ethnicinity, linked using the EthnicityID in the Tenant Tables and show the EthnicityName from the Host table

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I haven't done this before but probalby you can write yoru custom DbContextTypeMatcher and decide to use the correct DbContext.

    Currently this class creates a Host context or a Tenant context depending on MultiTenancySide determined here