Starts in:
1 DAY
10 HRS
46 MIN
10 SEC
Starts in:
1 D
10 H
46 M
10 S
Open Closed

Physically seperate DB for tenant #3334


User avatar
0
shobhit created
  • ABP Framework version: v4.2.2
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:"
  • I have to create physically seperate db for tenant. what are the steps.

10 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    When you creating a tenant, you can set the connection string for it.

  • User Avatar
    0
    shobhit created
    1. create seperate db for tenant
    2. run the db migrator project have default connection string pointing to tenant db
    3. Login to angular application as host admin and create new tenant and set the new db connection string
    4. Logout as host and select new tenant and try to login a. Cannot login as admin user created b. Cannot login as host admin default credentials
  • User Avatar
    0
    shobhit created

    Please note i am using 4.2.2

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can add a TenantChangedEventHandler to your domain project.

    public class TenantChangedEventHandler : ILocalEventHandler<EntityChangedEventData<Tenant>>, ITransientDependency
    {
        private readonly IEnumerable<I<YourProjectName>DbSchemaMigrator> _dbSchemaMigrators;
        private readonly ICurrentTenant _currentTenant;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IDataSeeder _dataSeeder;
        private readonly ITenantStore _tenantStore;
        private readonly ILogger<TenantChangedEventHandler> _logger;
    
        public TenantChangedEventHandler(
            IEnumerable<I<YourProjectName>DbSchemaMigrator> dbSchemaMigrators,
            ICurrentTenant currentTenant,
            IUnitOfWorkManager unitOfWorkManager,
            IDataSeeder dataSeeder, 
            ITenantStore tenantStore, 
            ILogger<TenantChangedEventHandler> logger)
        {
            _dbSchemaMigrators = dbSchemaMigrators;
            _currentTenant = currentTenant;
            _unitOfWorkManager = unitOfWorkManager;
            _dataSeeder = dataSeeder;
            _tenantStore = tenantStore;
            _logger = logger;
        }
    
        public async Task HandleEventAsync(EntityChangedEventData<Tenant> eventData)
        {
            await MigrateAndSeedForTenantAsync(
                eventData.Entity.Id
            );
        }
    
        private async Task MigrateAndSeedForTenantAsync(
            Guid tenantId)
        {
            try
            {
                using (_currentTenant.Change(tenantId))
                {
                    // Create database tables if needed
                    using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
                    {
                        var tenantConfiguration = await _tenantStore.FindAsync(tenantId);
                        if (tenantConfiguration?.ConnectionStrings != null &&
                            !tenantConfiguration.ConnectionStrings.Default.IsNullOrWhiteSpace())
                        {
                            foreach (var migrator in _dbSchemaMigrators)
                            {
                                await migrator.MigrateAsync();
                            }
                        }
    
                        await uow.CompleteAsync();
                    }
    
                    // Seed data
                    using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
                    {
                        await _dataSeeder.SeedAsync(
                            new DataSeedContext(tenantId)
                        );
    
                        await uow.CompleteAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
            }
        }
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    BTW, we recommend that you upgrade to the latest version, It contains lots of performance improvements and bug fixes as well as new features

  • User Avatar
    0
    shobhit created

    Hello liangshiwei, Thanks. what should be next step?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    That's all, when you create a tenant and set its connection string, you will see the database has been created

  • User Avatar
    0
    shobhit created

    ok. Thanks. Let me try

  • User Avatar
    0
    shobhit created

    When creating new tenant we don't provide connection string. After tenant creation, there is provision to provide new connection string.

  • User Avatar
    0
    shobhit created

    No Worries and Thanks for all help. I am able to setup and run the tenant in new DB

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06