Base solution for your next web application
Open Closed

CreateOrMigrateForTenant - How can be obtained tenantId #7221


User avatar
0
leonkosak created

CreateOrMigrateForTenant(AbpTenantBase tenant, Action<TDbContext> seedAction);

in migrator executer: _migrator.CreateOrMigrateForTenant(tenant, SeedHelperTenantCustom.SeedTenantDb);


public class SeedHelperTenantCustom
    {
        public static void SeedTenantDb(IIocResolver iocResolver)
        {
            WithDbContext<ThynkrDemoDbContext>(iocResolver, SeedTenantDb);
        }

        public static void SeedTenantDb(ThynkrDemoDbContext context)
        {
            new InitialTenantDbBuilderCustom(context).Create();
        }

        private static void WithDbContext<TDbContext>(IIocResolver iocResolver, Action<TDbContext> contextAction)
            where TDbContext : DbContext
        {
            using (var uowManager = iocResolver.ResolveAsDisposable<IUnitOfWorkManager>())
            {
                using (var uow = uowManager.Object.Begin(TransactionScopeOption.Suppress))
                {
                    var context = uowManager.Object.Current.GetDbContext<TDbContext>(MultiTenancySides.Tenant);

                    contextAction(context);

                    uow.Complete();
                }
            }
        }
    }
    

How can I obtain tenantId for current tenant context in SeedTenantDb method (action)? Thank you.


3 Answer(s)