Open Closed

How to set separate database for multi-tenant in microservice solution? #9717


User avatar
0
48949912 created

I have set the separate database connetionstring in page,but can't login use tenant?

  • Exception message and full stack trace: *when login by tenant,the exception show can't find demo.abpusers table ?
  • Steps to reproduce the issue:

22 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I have set the separate database connetionstring in page,

    can't find demo.abpusers table

    Can you share your settings from this page and full exception logs?

    Thanks.

  • User Avatar
    0
    48949912 created

    [maliming] said: hi

    I have set the separate database connetionstring in page,

    can't find demo.abpusers table

    Can you share your settings from this page and full exception logs?

    Thanks. default:Server=192.168.1.188;Port=3306;Database=SaaS_spc;Uid=root;Pwd=shrhkjgs@-@; AdministrationService Server=192.168.1.188;Port=3306;Database=SaaS_spcadmin;Uid=root;Pwd=shrhkjgs@-@ AuditLoggingService Server=192.168.1.188;Port=3306;Database=SaaS_spcaudit;Uid=root;Pwd=shrhkjgs@-@; LanguageService Server=192.168.1.188;Port=3306;Database=SaaS_spclang;Uid=root;Pwd=shrhkjgs@-@;

    After use tenant login,it give the exception:

  • User Avatar
    0
    48949912 created

    How to solve this problem?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Have you overridden the Login page?

    Have you set custom connection string for current tenants?

    Can you share the logs.txt of your localhost:44303 project?

    Thanks.

  • User Avatar
    0
    48949912 created

    [maliming] said: hi

    Have you overridden the Login page?

    Have you set custom connection string for current tenants?

    Can you share the logs.txt of your localhost:44303 project?

    Thanks.

    Excuse me , the last problem is solved, i can use tenant to login ,but i found new problem, when i click chat and file management, it given new exception:

  • User Avatar
    0
    48949912 created

    [48949912] said:

    [maliming] said: hi

    Have you overridden the Login page?

    Have you set custom connection string for current tenants?

    Can you share the logs.txt of your localhost:44303 project?

    Thanks.

    Excuse me , the last problem is solved, i can use tenant to login ,but i found new problem, when i click chat and file management, it given new exception: The DataBase Connection string for tenant user: default:Server=192.168.1.188;Port=3306;Database=SaaS_spc;Uid=root;Pwd=shrhkjgs@-@; AdministrationService Server=192.168.1.188;Port=3306;Database=SaaS_spcadmin;Uid=root;Pwd=shrhkjgs@-@ AuditLoggingService Server=192.168.1.188;Port=3306;Database=SaaS_spcaudit;Uid=root;Pwd=shrhkjgs@-@; LanguageService Server=192.168.1.188;Port=3306;Database=SaaS_spclang;Uid=root;Pwd=shrhkjgs@-@;

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Please share the logs.txt with liming.ma@volosoft.com

  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • In an ABP microservice solution, to use a separate database per tenant, you must ensure that:

      1. The tenant's connection string is set correctly in the SaaS management UI or via API.
      2. The database for the tenant is created and all required tables (e.g., AbpUsers) are present.
    • The error "can't find demo.abpusers table" indicates that the tenant's database does not have the necessary tables. This usually happens if database migrations have not been applied to the new tenant's database.

    Solution:

    1. Set the Tenant Connection String:

      • In the SaaS management UI, set the "Default" connection string for the tenant to point to a new, empty database.
    2. Apply Migrations to the Tenant Database:

      • ABP does not automatically run migrations for new tenant databases in microservice solutions. You must run the DbMigrator application to apply all module and application migrations to the tenant's database.
      • Run the DbMigrator after setting the connection string. This will create all required tables (including AbpUsers) in the tenant's database.

      Example command (from the root of your solution):

    dotnet run --project YourProject.DbMigrator
    
    • Ensure your connection strings and environment variables are set so the migrator can access all tenant databases.
    1. Verify Database:

      • After running the migrator, check the tenant's database to confirm that all tables are created.
    2. Retry Login:

      • Now, login as the tenant should work, as the required tables exist.

    Note: In microservice solutions, each service may have its own DbMigrator. You must run the migrator for each service that uses a database.

    References:


    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

    The error is

    MySqlConnector.MySqlException (0x80004005): Table 'saas_spc.chatusers' doesn't exist

    default:Server=192.168.1.188;Port=3306;Database=SaaS_spc;Uid=root;Pwd=shrhkjgs@-@;
    AdministrationService Server=192.168.1.188;Port=3306;Database=SaaS_spcadmin;Uid=root;Pwd=shrhkjgs@-@
    AuditLoggingService Server=192.168.1.188;Port=3306;Database=SaaS_spcaudit;Uid=root;Pwd=shrhkjgs@-@;
    LanguageService Server=192.168.1.188;Port=3306;Database=SaaS_spclang;Uid=root;Pwd=shrhkjgs@-@;
    

    Can you check the tables in the SaaS_spc database?

    Thanks.

  • User Avatar
    0
    48949912 created

    [maliming] said: hi

    The error is

    MySqlConnector.MySqlException (0x80004005): Table 'saas_spc.chatusers' doesn't exist

    default:Server=192.168.1.188;Port=3306;Database=SaaS_spc;Uid=root;Pwd=shrhkjgs@-@; 
    AdministrationService Server=192.168.1.188;Port=3306;Database=SaaS_spcadmin;Uid=root;Pwd=shrhkjgs@-@ 
    AuditLoggingService Server=192.168.1.188;Port=3306;Database=SaaS_spcaudit;Uid=root;Pwd=shrhkjgs@-@; 
    LanguageService Server=192.168.1.188;Port=3306;Database=SaaS_spclang;Uid=root;Pwd=shrhkjgs@-@; 
    

    Can you check the tables in the SaaS_spc database?

    Thanks.

    Excuse me,i can‘t find the table  “chatusers” in the database “saas_spc”,not create after set tenant separate database connectionstring  by the page。

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    What are the connection strings of the current tenant?

    You can try creating a new tenant with a custom connection string, then check the logs.txt file to see if the tenant migration is successful.

    Thanks.

  • User Avatar
    0
    48949912 created

    [maliming] said: What are the connection strings of the current tenant?

    You can try creating a new tenant with a custom connection string, then check the logs.txt file to see if the tenant migration is successful.

    Thanks.

    I have Create New Microservice Solution? I create a new tenant:sungrow,The tenant separate database connectionstring like this:

    After set tenant separate database connectionstring and click 'save' button,I have send the logs to liming.ma@volosoft.com

    After login use tenant,throw the exception:

    So I Check the tenant database,the Table 'imcsspc sungrow.abpusers' doesn't exist:

    It's really urgent - been stuck for days without resolution!

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    After login use tenant,throw the exception:

    Can you share the logs after this exception?

    I checked the logs. The databases have migrated successfully.

    Thanks.

  • User Avatar
    0
    48949912 created

    [maliming] said: hi

    After login use tenant,throw the exception:

    Can you share the logs after this exception?

    I checked the logs. The databases have migrated successfully.

    Thanks.

    I have send the logs to liming.ma@volosoft.com

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Thanks. I'm checking them.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I found the problem. I will provide a solution soon.

    Thanks.

  • User Avatar
    0
    48949912 created

    [maliming] said: hi

    I found the problem. I will provide a solution soon.

    Thanks.

    Thanks a lot!

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Try to update XXXSaasServiceModule and SaasServiceDatabaseMigrationEventHandler as below:

    Then set connection strings for each microserver in a tenant.

    Configure<AbpDbConnectionOptions>(options =>
    {
        options.Databases.Configure("AdministrationService", database =>
        {
            database.MappedConnections.Add(AbpPermissionManagementDbProperties.ConnectionStringName);
            database.MappedConnections.Add(AbpFeatureManagementDbProperties.ConnectionStringName);
            database.MappedConnections.Add(AbpSettingManagementDbProperties.ConnectionStringName);
        });
    
        options.Databases.Configure("AuditLoggingService", database =>
        {
            database.MappedConnections.Add(AbpAuditLoggingDbProperties.ConnectionStringName);
        });
    
        options.Databases.Configure("ChatService", database =>
        {
            database.MappedConnections.Add("ChatService");
        });
    
        options.Databases.Configure("GdprService", database =>
        {
            database.MappedConnections.Add("GdprService");
        });
    
        options.Databases.Configure("IdentityService", database =>
        {
            database.MappedConnections.Add("IdentityService");
        });
    
        options.Databases.Configure("FileManagementService", database =>
        {
            database.MappedConnections.Add("FileManagementService");
        });
    
        options.Databases.Configure("LanguageService", database =>
        {
            database.MappedConnections.Add(LanguageManagementDbProperties.ConnectionStringName);
        });
    });
    
    using Volo.Abp.EntityFrameworkCore.Migrations;
    using Volo.Abp.EventBus.Distributed;
    using Volo.Abp.DistributedLocking;
    using Volo.Abp.MultiTenancy;
    using Volo.Abp.Uow;
    using Volo.Abp.Data;
    
    namespace AbpSolution2.SaasService.Data;
    
    public class SaasServiceDatabaseMigrationEventHandler : EfCoreDatabaseMigrationEventHandlerBase<SaasServiceDbContext>
    {
        private readonly SaasServiceDataSeeder _dataSeeder;
    
        public SaasServiceDatabaseMigrationEventHandler(
            ILoggerFactory loggerFactory,
            ICurrentTenant currentTenant,
            IUnitOfWorkManager unitOfWorkManager,
            ITenantStore tenantStore,
            IAbpDistributedLock abpDistributedLock,
            IDistributedEventBus distributedEventBus,
            SaasServiceDataSeeder dataSeeder
        ) : base(
            SaasServiceDbContext.DatabaseName,
            currentTenant,
            unitOfWorkManager,
            tenantStore,
            abpDistributedLock,
            distributedEventBus,
            loggerFactory)
        {
            _dataSeeder = dataSeeder;
        }
    
        public override Task HandleEventAsync(TenantCreatedEto eventData)
        {
            return Task.CompletedTask;
        }
    
        public override Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData)
        {
            return Task.CompletedTask;
        }
    
        public override Task HandleEventAsync(TenantConnectionStringUpdatedEto eventData)
        {
            return Task.CompletedTask;
        }
    
        protected override async Task SeedAsync(Guid? tenantId)
        {
            await _dataSeeder.SeedAsync(tenantId);
        }
    }
    
  • User Avatar
    0
    48949912 created

    [maliming] said: hi

    Try to update XXXSaasServiceModule and SaasServiceDatabaseMigrationEventHandler as below:

    Then set connection strings for each microserver in a tenant.

    Configure<AbpDbConnectionOptions>(options => 
    { 
        options.Databases.Configure("AdministrationService", database => 
        { 
            database.MappedConnections.Add(AbpPermissionManagementDbProperties.ConnectionStringName); 
            database.MappedConnections.Add(AbpFeatureManagementDbProperties.ConnectionStringName); 
            database.MappedConnections.Add(AbpSettingManagementDbProperties.ConnectionStringName); 
        }); 
     
        options.Databases.Configure("AuditLoggingService", database => 
        { 
            database.MappedConnections.Add(AbpAuditLoggingDbProperties.ConnectionStringName); 
        }); 
     
        options.Databases.Configure("ChatService", database => 
        { 
            database.MappedConnections.Add("ChatService"); 
        }); 
     
        options.Databases.Configure("GdprService", database => 
        { 
            database.MappedConnections.Add("GdprService"); 
        }); 
     
        options.Databases.Configure("IdentityService", database => 
        { 
            database.MappedConnections.Add("IdentityService"); 
        }); 
     
        options.Databases.Configure("FileManagementService", database => 
        { 
            database.MappedConnections.Add("FileManagementService"); 
        }); 
     
        options.Databases.Configure("LanguageService", database => 
        { 
            database.MappedConnections.Add(LanguageManagementDbProperties.ConnectionStringName); 
        }); 
    }); 
    
    using Volo.Abp.EntityFrameworkCore.Migrations; 
    using Volo.Abp.EventBus.Distributed; 
    using Volo.Abp.DistributedLocking; 
    using Volo.Abp.MultiTenancy; 
    using Volo.Abp.Uow; 
    using Volo.Abp.Data; 
     
    namespace AbpSolution2.SaasService.Data; 
     
    public class SaasServiceDatabaseMigrationEventHandler : EfCoreDatabaseMigrationEventHandlerBase<SaasServiceDbContext> 
    { 
        private readonly SaasServiceDataSeeder _dataSeeder; 
     
        public SaasServiceDatabaseMigrationEventHandler( 
            ILoggerFactory loggerFactory, 
            ICurrentTenant currentTenant, 
            IUnitOfWorkManager unitOfWorkManager, 
            ITenantStore tenantStore, 
            IAbpDistributedLock abpDistributedLock, 
            IDistributedEventBus distributedEventBus, 
            SaasServiceDataSeeder dataSeeder 
        ) : base( 
            SaasServiceDbContext.DatabaseName, 
            currentTenant, 
            unitOfWorkManager, 
            tenantStore, 
            abpDistributedLock, 
            distributedEventBus, 
            loggerFactory) 
        { 
            _dataSeeder = dataSeeder; 
        } 
     
        public override Task HandleEventAsync(TenantCreatedEto eventData) 
        { 
            return Task.CompletedTask; 
        } 
     
        public override Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData) 
        { 
            return Task.CompletedTask; 
        } 
     
        public override Task HandleEventAsync(TenantConnectionStringUpdatedEto eventData) 
        { 
            return Task.CompletedTask; 
        } 
     
        protected override async Task SeedAsync(Guid? tenantId) 
        { 
            await _dataSeeder.SeedAsync(tenantId); 
        } 
    } 
    

    Thanks a lot! I I'm going to test right away.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, you can create a new tenant and set all service connection strings.

    Thanks.

  • User Avatar
    0
    48949912 created

    [maliming] said: ok, you can create a new tenant and set all service connection strings.

    Thanks.

    According to the method you provided above, it can currently be used normally. You are professional!

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Actually, we don't recommend setting independent connection strings for tenants in microservices. However, you can try it and feel free to provide feedback if you encounter any problems.

    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.0.0-preview. Updated on September 01, 2025, 08:37