Base solution for your next web application
Open Closed

Can use multi dbcontext & multi tenancy in the same time? #2790


User avatar
0
trendline created

Can I use multi DbContext (in fact, it is multi database instance) and multi tenancy per database in the same time? Is that will create multi database instance for each tenant?


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

    Hi,

    I'm asking just to be sure,

    This is possible. Let's say that you have 2 dbContext and you want to use DB per tenant. In that case you can store 2 DbContexes for a tenant in a single database, is that what you want ?

    Thanks.

  • User Avatar
    0
    trendline created

    In my scenario multi DbContext means multi database, then one tenant will store data in multi database.

    Furthermore, If I can configure the tenant as separated database?

    For example, one tenant with 2 DbContext, and it mapping 2 separated database, Db_A and Db_B. Then I configure the tenant as separated database in Host configuration, does that will generate Db_A_Tenant1, Db_B_Tenant1 for Teanant1 and Db_A_Tenant2, Db_B_Tenant2 for Tenant2?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    This is not supported by default because a Tenant has a single connection string property.

    But you can make it like this

    • You can store multiple connection strings seperated with pipe (orany other char) for a tenant.
    • Then you need to implement a similar class like this one <a class="postlink" href="https://github.com/aspnetboilerplate/module-zero/blob/master/src/Abp.Zero.EntityFramework/Zero/EntityFramework/DbPerTenantConnectionStringResolver.cs">https://github.com/aspnetboilerplate/mo ... esolver.cs</a>. So you will get correct connection string for your tenant & db context from the connection string seperated with pipe.

    Please let us know if you face any problems while doing it.

    Thanks.

  • User Avatar
    0
    trendline created

    Thanks your suggestion, I will take a try and let you know the result. But, a little confuses, Can current Tenant per Db setting generate multi database automatically? Otherwise, need to spend time to extend it.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Yes, it creates a seperate DB but it creates a single DB for a single connection string. In your case you want to create two seperate database for each tenant, an I right ? If so, you need to change the logic for finding correct connection string for given db context.

    I will be waiting for your feedback.