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)
-
0
-
0
- create seperate db for tenant
- run the db migrator project have default connection string pointing to tenant db
- Login to angular application as host admin and create new tenant and set the new db connection string
- 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
-
0
Please note i am using 4.2.2
-
0
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); } } }
-
0
BTW, we recommend that you upgrade to the latest version, It contains lots of performance improvements and bug fixes as well as new features
-
0
Hello liangshiwei, Thanks. what should be next step?
-
0
Hi,
That's all, when you create a tenant and set its connection string, you will see the database has been created
-
0
ok. Thanks. Let me try
-
0
-
0
No Worries and Thanks for all help. I am able to setup and run the tenant in new DB