Open Closed

How to create Tenant database from within application #204


User avatar
0
edirkzwager created

Check the docs before asking a question: https://docs.abp.io/en/commercial/latest/ Check the samples, to see the basic tasks: https://docs.abp.io/en/commercial/latest/samples/index The exact solution to your question may have been answered before, please use the search on the homepage.

  • ABP Framework version: v2.8.0
  • UI type: ~~Angular ~~/ MVC
  • Tiered (MVC) ~~or Identity Server Seperated (Angular)~~: yes / ~~no~~
  • Exception message and stack trace: None
  • Steps to reproduce the issue:
  • We are trying to create the tenant database from within the aplication. First we override the CreateAsync function. Within that function we create the tenant and create a custom connection string. The last step we want to do is to create the database itself but here we are struggling. What is the correct and best way to call the migrator for the new tenant. The code below shows how we have currently tried to solve this. We added the dependancy to the DBmigrator and made the functions public so we can access it. We do not get any error, the database is just not created. The connecution string is called default and we only want to create the new database for the new tenant.
namespace Kantan
{
	[Dependency(ReplaceServices = true)]
	[ExposeServices(typeof(ITenantAppService), typeof(KantanTenantAppService))]
	public class KantanTenantAppService : TenantAppService
	{
		private readonly ITenantRepository _tenantRepository;
		private readonly IEditionRepository _editionRepository;
		private readonly ITenantManager _tenantManager;
		private readonly IDataSeeder _dataSeeder;
		public KantanTenantAppService(ITenantRepository tenantRepository,
        IEditionRepository editionRepository,
        ITenantManager tenantManager,
        IDataSeeder dataSeeder ) : base(
            tenantRepository,
            editionRepository,
            tenantManager,
            dataSeeder)
		{
			_tenantRepository = tenantRepository;
			_editionRepository = editionRepository;
			_tenantManager = tenantManager;
			_dataSeeder = dataSeeder;
		}
		[Authorize("Saas.Tenants.Create")]
		public override async Task<SaasTenantDto> CreateAsync(SaasTenantCreateDto input)
		{
			SaasTenantDto result;
			string newConnectionString = "";
			Tenant tenant;

			result = await base.CreateAsync(input);
			tenant = _tenantRepository.FindByName(input.Name);
			if (tenant!=null)
			{
				newConnectionString = $"Server=(LocalDb)\\MSSQLLocalDB;Database=Kantan{input.Name};Trusted_Connection=True;MultipleActiveResultSets=true";
				await base.UpdateDefaultConnectionStringAsync(tenant.Id, newConnectionString);

				//await ServiceProvider.
				//	GetRequiredService<KantanDbMigrationService>()
				//	.MigrateHostDatabaseAsync();
				await ServiceProvider.
					GetRequiredService<KantanDbMigrationService>()
					.MigrateTenantDatabasesAsync(tenant);
			}
			return result;
		}
	}
}

2 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Try:

    using (_currentTenant.Change(tenant.Id))
    {
        await ServiceProvider.
            GetRequiredService<KantanDbMigrationService>()
            .MigrateTenantDatabasesAsync(tenant);
    }
    
  • User Avatar
    0
    edirkzwager created

    Thanks liangshiwei. Got it working !

Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15