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

Create/Update product defined EF model instances for each tenant #9696


User avatar
0
atideveloper created

I am using v9.1.0, .NET Core and Angular.

We have a multi-tenant application that uses multiple databases with 1+ tenants per DB. We have a an EF model that must have a tenant and can contain many entries per tenant.

The problem is that we need some of the instances to be created by us while allowing other instances to be created by each tenant. The tenants will only see what we created for them and what they created themselves - they wont see what other tenants created.

For the instances that we create they will be exactly the same for every tenant. It will not be a different set of objects we create for each tenant and we will not be creating them manually for a specific tenant - they will be created when a new tenant is created or when we install a new release of our application.

What I am trying to figure out is a good way for us to create and add new entries that we want to control. So, for example, when a new tenant is created we want to create several entries for that tenant. Also, when we install an upgrade we may want to add more instances, or modify previous instances, so we need to go through all existing tenants and create/modify them.

I see where I can hook into the tenant creation process (TenantManger.CreateWithAdminUserAsync) so that should work for new tenants. But, when it comes to adding or changing data for existing tenants when we install a new version of our software I am at a loss. Migrations might be an option, but the default Migrator application only applies migrations once to a DB so for multiple tenants per DB it would not work.

Are there any built in mechanism for handling the creation/updating of tenant specific data when a new tenant is created or an upgrade is applied? Should I try to modify the Migrator application to have it update existing tenants with new data?

Any thoughts or suggestions on how best to do this would be appreciated.

Thanks!


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

    Hi @atideveloper

    Using migrations for the second scenario is a good approach I guess. The DbMigrator project in the solution should apply migrations to all databases including the separate tenant databases as well. Isn't it working like that for you ?

  • User Avatar
    0
    atideveloper created

    Thanks. Yea, the DbMigrator project is working but the issue is it only applies the migrations once per database. So with multiple tenants in a single DB I will need to find a way to add new entity instances for each tenant. So something like a foreach(tenant) addTenantSpecificData(); would have to be executed once for each DB after the migrations have been applied I guess.

    I did notice several creator classes within Migrations.Seed.Tenants so maybe creating one of those would work? I assume those will get applied after the DB migrations so any schema changes will be applied. I could then go through each tenant in the DB being updated and add any/modify any custom data for them.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Yes, that's right. You can modify this section of the Migrator (https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Migrator/MultiTenantMigrateExecuter.cs#L88) and execute _migrator.CreateOrMigrateForTenant(tenant); for each separate database as it works now and add your seeding code into the foreach block. In that way, you can execute seed for each tenant in a separate database.

    I guess, you also need to do the same for host database as well for each tenant on host database.