Hi -
I am creating a new database for each tenant. I would like to delete the database once the tenant is deleted. Where and what is the best way to implement this ?
3 Answer(s)
-
0
Hi,
We didn't implement the drop database process because the tenant is being soft deleted. So you can revert back the deletion in the database. But despite wanting to do that, I created a gist. I drop the database right after the tenant is being deleted in TenantAppService.cs > DeleteTenant() method.
[AbpAuthorize(AppPermissions.Pages_Tenants)] public class TenantAppService : AbpZeroTemplateAppServiceBase, ITenantAppService { public IAppUrlService AppUrlService { get; set; } //Inject necessary classes private readonly IDbContextResolver _dbContextResolver; private readonly IDbPerTenantConnectionStringResolver _connectionStringResolver; //... public TenantAppService(IDbContextResolver dbContextResolver, IDbPerTenantConnectionStringResolver connectionStringResolver) { _dbContextResolver = dbContextResolver; _connectionStringResolver = connectionStringResolver; AppUrlService = NullAppUrlService.Instance; } //My new drop database method private async Task DropTenantDatabase(AbpTenantBase tenant) { var args = new DbPerTenantConnectionStringResolveArgs(tenant.Id, MultiTenancySides.Tenant) { ["DbContextType"] = typeof(AbpZeroTemplateDbContext), ["DbContextConcreteType"] = typeof(AbpZeroTemplateDbContext) }; var connectionString = ConnectionStringHelper.GetConnectionString( _connectionStringResolver.GetNameOrConnectionString(args) ); using (var dbContext = _dbContextResolver.Resolve<AbpZeroTemplateDbContext>(connectionString, null)) { await dbContext.Database.EnsureDeletedAsync(); } } //I use the DropTenantDatabase() right after TenantManager.DeleteAsync(); [AbpAuthorize(AppPermissions.Pages_Tenants_Delete)] public async Task DeleteTenant(EntityDto input) { var tenant = await TenantManager.GetByIdAsync(input.Id); await TenantManager.DeleteAsync(tenant); await DropTenantDatabase(tenant); }
See the gist : <a class="postlink" href="https://gist.github.com/ebicoglu/6fe14f65a21713f673ba5b55e94bb682#file-tenantappservice-cs-L111">https://gist.github.com/ebicoglu/6fe14f ... ce-cs-L111</a>
[attachment=0:23vi0rjb]DropTenantDbWhenTenantIsDeleted.jpg[/attachment:23vi0rjb]
-
0
Works like a charm.. Thank you so much.
-
0
Great! ;)