Open Closed

Adding new DBs for each created tenant for each ServiceModule(feature) #8949


User avatar
0
Ehab@Siemat created

ABP Framework version: v9.0.4

  • UI Type: Angular

  • Database System: EF Core (PostgreSQL)

Hi,

We are currently working on a solution that involves Editions, where each Edition should include specific Features. These Features are to be represented as service modules in our architecture.
We intend to set up dynamically a separate database for each tenant for every service module they are using.

What is the best practice to implement this please?

Thanks


4 Answer(s)
  • User Avatar
    0
    Ehab@Siemat created

    and if i have an EditionId, how to get the features related to it?

  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi,

    ABP Framework allows tenants to use separate databases and Saas module has an UI to separate tenant databases manually at runtime. But there is no built-in feature for creating databases dynamically whenever a tenant created or edition changes for a tenant. You have to implement it in your application according to your logic.

    Let me show the TenantAppService.cs implementation from the Saas Module:

    image.png

    If you create each tenant with ConnectionStrings property is configured and make sure distributed event is published, you can achieve this logic.

    You don't have to write all the codes here, I just wanted to explain the logic. You can easily replace TenantAppService in your application and configure connection string for each tenant dynamically:

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(ITenantAppService))]
    public class CustomTenantAppService : TenantAppService
    {
        public CustomTenantAppService(ITenantRepository tenantRepository, IEditionRepository editionRepository, ITenantManager tenantManager, IDataSeeder dataSeeder, ILocalEventBus _localEventBus, IDistributedEventBus distributedEventBus, IOptions<AbpDbConnectionOptions> dbConnectionOptions, IConnectionStringChecker connectionStringChecker) : base(tenantRepository, editionRepository, tenantManager, dataSeeder, _localEventBus, distributedEventBus, dbConnectionOptions, connectionStringChecker)
        {
        }
    
        public override Task<SaasTenantDto> CreateAsync(SaasTenantCreateDto input)
        {
            // 👇 Something similar
            input.ConnectionStrings.Default = $"Server=localhost;Database=MyProjectName_{input.Name.Normalize()};User=root;Password=123456;";
            return base.CreateAsync(input);
        }
    }
    
  • User Avatar
    0
    Ehab@Siemat created

    I have two questions plz:

    1- How can we specify which service tenant database a microservice should work with (e.g., Microservice_Tenant1_Products, Microservice_Tenant2_Products, etc.)?

    2- When we add new database migrations at the service, how should we handle them considering that each tenant has its own separate service database? how this migration will be applied to all service tenant dbs?

  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi

    How can we specify which service tenant database a microservice should work with (e.g., Microservice_Tenant1_Products, Microservice_Tenant2_Products, etc.)?

    Do you use micro-serivce template currently or you'll separate your services later? By default we don't suggest using different databases for tenants in the micro-service template currently, if you wish you can rea the discussion from here: https://abp.io/support/questions/8692/Problems-configuring-a-separate-database-for-each-tenant-in-a-microservices-application

    If your template is not micro-service, you can customize MultiTenantConnectionStringResolver in your application

    https://github.com/abpframework/abp/blob/74d516829be7f05cfae7d4a67f18591b41e5446a/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenantConnectionStringResolver.cs#L76-L79

    using System;
    using Microsoft.Extensions.Options;
    using Volo.Abp.Data;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.MultiTenancy;
    
    namespace AbpSolution1.AuthServer;
    
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IConnectionStringResolver), typeof(DefaultConnectionStringResolver))]
    public class MicroServiceConnectionStringResolver : MultiTenantConnectionStringResolver
    {
        private readonly ICurrentTenant _currentTenant;
        private readonly IServiceProvider _serviceProvider;
        public MicroServiceConnectionStringResolver(IOptionsMonitor<AbpDbConnectionOptions> options, ICurrentTenant currentTenant, IServiceProvider serviceProvider) : base(options, currentTenant, serviceProvider)
        {
            _currentTenant = currentTenant;
            _serviceProvider = serviceProvider;
        }
    
        public override async Task<string> ResolveAsync(string? connectionStringName = null)
        {
            // ⚠️ Implement Your own logic, this is for demonstration
            var prefix = "Microservice";
            var postfix="Products";
            var tenant = _currentTenant.Name;
    
            // ...
            
            return $"Server=myServerAddress;Database={prefix}_{tenant}_{postfix};Trusted_Connection=True;"
    
        }
    }
    

    When we add new database migrations at the service, how should we handle them considering that each tenant has its own separate service database? how this migration will be applied to all service tenant dbs?

    DbMigrator project applies migrations for all the tenants in the host database. So whenever you run DbMigrator, all the databases will be migrated and data seed will be executed

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 13, 2025, 04:08