Starts in:
2 DAYS
0 HR
46 MIN
47 SEC
Starts in:
2 D
0 H
46 M
47 S
Open Closed

Testing module in multitenant #3791


User avatar
0
andmattia created

ABP 5.3.1 Anuglar

I've create a new module now I need to test in multitenant.

I switch the tenant switch on API side but when I try to create a new tenant doesn't create the admin user


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

    hi

    Can you share your details steps?

  • User Avatar
    0
    andmattia created

    Hi

    step to reproduce:

    1. create new empty module (v 5.3.1 with Angular UI and separated IDS)
    2. set multi tenant = true on shared module
    3. start IDS+API+UI
    4. login as admin
    5. create a new tenant
    6. try to login with in the new tenant

    You get an error because admin user doesn't exists. So if you look into db the user admin for tenant not exists

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Can you check your app(IDS+API) logs? Are there any errors?

  • User Avatar
    0
    andmattia created

    No I don't have any error and tenant exists

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you try with a new template? 5.3.4 or 6.0.0-rc.5

  • User Avatar
    0
    andmattia created

    ok I can try with 5.3.4

  • User Avatar
    0
    andmattia created

    I try in 5.3.4 same result!

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, Please share your test project with me liming.ma@volosoft.com

  • User Avatar
    0
    andmattia created

    I send the solution via wetransfert

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I don't receive the mail. Can you check?

  • User Avatar
    0
    andmattia created

    Hi

    I check it and resend via weTransfer. Let me know if you don't receive

  • User Avatar
    0
    andmattia created

    Hi Any update?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I will check it asap.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Please create a same name app-pro project and copy MyProjectNameTenantDatabaseMigrationHandler to your Dm.DynamicTemplate.IdentityServer project.

  • User Avatar
    0
    andmattia created

    Hi I try but miss a lot of function like saas. It s not complete solution for test module in saas

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I will share the code. Please wait.

  • User Avatar
    0
    andmattia created

    Any update?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    sorry for delay, I will share code asap

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Add MyProjectNameTenantDatabaseMigrationHandler to your Dm.DynamicTemplate.IdentityServer project.

    using System;
    using System.Threading.Tasks;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Logging;
    using MyCompanyName.MyProjectName.EntityFrameworkCore;
    using Volo.Abp.Data;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.EntityFrameworkCore;
    using Volo.Abp.EventBus.Distributed;
    using Volo.Abp.MultiTenancy;
    using Volo.Abp.Uow;
    
    namespace MyCompanyName.MyProjectName.Data;
    
    public class MyProjectNameTenantDatabaseMigrationHandler :
        IDistributedEventHandler<TenantCreatedEto>,
        IDistributedEventHandler<TenantConnectionStringUpdatedEto>,
        IDistributedEventHandler<ApplyDatabaseMigrationsEto>,
        ITransientDependency
    {
        private readonly IDbContextProvider<AuthServerHostMigrationsDbContext> _dbContextProvider;
        private readonly ICurrentTenant _currentTenant;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IDataSeeder _dataSeeder;
        private readonly ITenantStore _tenantStore;
        private readonly ILogger<MyProjectNameTenantDatabaseMigrationHandler> _logger;
    
        public MyProjectNameTenantDatabaseMigrationHandler(
            IDbContextProvider<AuthServerHostMigrationsDbContext> dbContextProvider,
            ICurrentTenant currentTenant,
            IUnitOfWorkManager unitOfWorkManager,
            IDataSeeder dataSeeder,
            ITenantStore tenantStore,
            ILogger<MyProjectNameTenantDatabaseMigrationHandler> logger)
        {
            _dbContextProvider = dbContextProvider;
            _currentTenant = currentTenant;
            _unitOfWorkManager = unitOfWorkManager;
            _dataSeeder = dataSeeder;
            _tenantStore = tenantStore;
            _logger = logger;
        }
    
        public async Task HandleEventAsync(TenantCreatedEto eventData)
        {
            await MigrateAndSeedForTenantAsync(eventData.Id);
        }
    
        public async Task HandleEventAsync(TenantConnectionStringUpdatedEto eventData)
        {
            if (eventData.ConnectionStringName != ConnectionStrings.DefaultConnectionStringName ||
                eventData.NewValue.IsNullOrWhiteSpace())
            {
                return;
            }
    
            await MigrateAndSeedForTenantAsync(eventData.Id);
    
            /* You may want to move your data from the old database to the new database!
             * It is up to you. If you don't make it, new database will be empty
             * (and tenant's admin password is reset to 1q2w3E*).
             */
        }
    
        public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData)
        {
            if (eventData.TenantId == null)
            {
                return;
            }
    
            await MigrateAndSeedForTenantAsync(eventData.TenantId.Value);
        }
    
        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())
                        {
    
                            await (await _dbContextProvider.GetDbContextAsync()).Database.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);
            }
        }
    }
    
    
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06