Base solution for your next web application
Open Closed

Run Seed Data on the new Tenant Databases #12477


User avatar
0
[email protected] created

I want to execute the seeding in the new tenant databases. I tried modifying the MultiTenantMigrateExecuter as follows:

        if (!migratedDatabases.Contains(tenant.ConnectionString))
        {
            try
            {
                // seed helper is newly added to execute seed data to the tenant datbases also.
                _migrator.CreateOrMigrateForTenant(tenant, SeedHelper.SeedTenantDb);
            }
            catch (Exception ex)
            {
                Log.Write("An error occured during migration of tenant database:");
                Log.Write(ex.ToString());
                Log.Write("Skipped this tenant and will continue for others...");
            }

            migratedDatabases.Add(tenant.ConnectionString);
        }
        

SeedTenantDb:

    public static void SeedTenantDb(ABMDbContext context)
    {
        context.SuppressAutoSetTenantId = false;
        // seed tenant databases
        new SeedTenantDatabases(context).Create();
    }

When I run the Migrator.exe, it returns the following error:

I tried another option to run the seed soon after creating a new Tenant. I tried to make changes in the TenantManager as follows:

        //Create tenant database
        _abpZeroDbMigrator.CreateOrMigrateForTenant(tenant, SeedHelper.SeedTenantDb);

But I cannot add the reference to the MyProject.Migrations in the MyProject.Core project. Even if I add it, it requires having the tenant ID to be set for the entities. How can we achieve this?

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

3 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi

    Can you try the first situation you mentioned, that is, by making changes to the MultiTenantMigrateExecuter class in the Migrator project as shown below? I assume that the SeedTenantDb action is suitable for tenant seeding.

    try
    {
        _migrator.CreateOrMigrateForTenant(tenant, context =>
        {
            using (context.AbpSession.Use(tenant.Id, null))
            {
                SeedHelper.SeedTenantDb(context);
            }
        });
    }
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    [email protected] created

    Yeah, I tried that now. But the result is the same error.

    Can we do it as I mentioned above, through the TenantManager while creating the new tenant? So that we don't need to run the exe in production just for creating a new tenant.

    But still, referring to the SeedHelper and setting the tenant ID is a problem for me there.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    [email protected] created

    I achieved the result by modifying your code in TenantManager as follows:

    I moved the _abpZeroDbMigrator.CreateOrMigrateForTenant(tenant); inside to the unit of work code block, which sets the tenant ID.

    Also, I've overridden the CreateOrMigrateForTenant method in AbpZeroDbMigrator as follows:

    public override void CreateOrMigrateForTenant(AbpTenantBase tenant)
    {
        CreateOrMigrateForTenant(tenant, SeedHelper.SeedTenantDb);
    }
    

    This has given me the desired output. Now, whenever I create a new tenant with the new database, it will have the seed executed.

    Hope it doesn't have any counter effects. Looking forward to your advice.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)