Open Closed

Tenant seeder run twice #10533


User avatar
0
ahmed.codeator created

[Authorize(TenantManagementPermissions.Tenants.Create)] public virtual async Task<TenantDto> CreateAsync(TenantCreateDto input) { var tenant = await TenantManager.CreateAsync(input.Name); input.MapExtraPropertiesTo(tenant);

await TenantRepository.InsertAsync(tenant);

await CurrentUnitOfWork.SaveChangesAsync();

await DistributedEventBus.PublishAsync(
    new TenantCreatedEto
    {
        Id = tenant.Id,
        Name = tenant.Name,
        Properties =
        {
                { "AdminEmail", input.AdminEmailAddress },
                { "AdminPassword", input.AdminPassword }
        }
    });

using (CurrentTenant.Change(tenant.Id, tenant.Name))
{
    //TODO: Handle database creation?
    // TODO: Seeder might be triggered via event handler.
    await DataSeeder.SeedAsync(
                    new DataSeedContext(tenant.Id)
                        .WithProperty("AdminEmail", input.AdminEmailAddress)
                        .WithProperty("AdminPassword", input.AdminPassword)
                    );
}

return ObjectMapper.Map&lt;Tenant, TenantDto&gt;(tenant);

}

this is seeding twice one explicitly and one in event hander and this is creating all kind of issues for the second one alot of errors are reported and the tenant creation become too slow the data is seeded but the second one is unnecessary and making things very slow how can we solve that


5 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • Remove your explicit DataSeeder.SeedAsync(...) call from CreateAsync.
      In ABP, tenant creation already triggers seeding via the tenant-created flow (you’re also publishing TenantCreatedEto, and your system has an event handler that seeds). Calling DataSeeder.SeedAsync in the same method guarantees a duplicate seed run and will cause slowness + “already exists” type errors.

      So change your method to:

    [Authorize(TenantManagementPermissions.Tenants.Create)]
      public virtual async Task<TenantDto> CreateAsync(TenantCreateDto input)
      {
          var tenant = await TenantManager.CreateAsync(input.Name);
          input.MapExtraPropertiesTo(tenant);
    
          await TenantRepository.InsertAsync(tenant);
          await CurrentUnitOfWork.SaveChangesAsync();
    
          await DistributedEventBus.PublishAsync(
              new TenantCreatedEto
              {
                  Id = tenant.Id,
                  Name = tenant.Name,
                  Properties =
                  {
                      { "AdminEmail", input.AdminEmailAddress },
                      { "AdminPassword", input.AdminPassword }
                  }
              });
    
          return ObjectMapper.Map<Tenant, TenantDto>(tenant);
      }
    
    • Put all tenant-initialization logic into the event handler (single place).
      The event handler should:

      1. switch tenant (using (CurrentTenant.Change(...)))
      2. run migrations if you do separate DB per tenant
      3. call DataSeeder.SeedAsync(new DataSeedContext(tenantId).WithProperty(...))

      This ensures it runs exactly once and can be executed reliably even if tenant creation happens from another service/process.

    • If you want it synchronous (not recommended), then don’t publish TenantCreatedEto.
      That would stop the handler from running, but you lose the standard “tenant created” integration behavior. The recommended ABP approach is the opposite: keep the event, remove the manual seeding.

    Links (validated):

    • https://abp.io/docs/latest/framework/infrastructure/data-seeding
    • https://abp.io/docs/latest/framework/architecture/multi-tenancy#change-the-current-tenant

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    ahmed.codeator created

    this doesn't solve it

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Hi, is the code you shared from the ABP module, or is it your own custom code?

    Thanks.

  • User Avatar
    0
    ahmed.codeator created

    abp module TenantAppService in Volo.Abp.TenantManagement

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Volo.Abp.TenantManagement is an open-source module. You have a commercial license; you should use Volo.Saas.Host.Application

    But I will check the open source side as well.

    Thanks.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.3.0-preview. Updated on March 13, 2026, 12:51
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.