Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Delete the tenant database while deleting the Tenant #4991


User avatar
0
maharatha created

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)
  • User Avatar
    0
    alper created
    Support Team

    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]

  • User Avatar
    0
    maharatha created

    Works like a charm.. Thank you so much.

  • User Avatar
    0
    alper created
    Support Team

    Great! ;)