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?
3 Answer(s)
-
0
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 supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Yeah, I tried that now. But the result is the same error.
Can we do it as I mentioned above, through the
TenantManagerwhile 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
SeedHelperand setting the tenant ID is a problem for me there.Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
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 supportedCopy & paste or drag & drop images (max 30 MB per image)