Base solution for your next web application
Open Closed

New Tenant - Migrator #2139


User avatar
0
hugol created

Hello,

I couldn't find a thread related to this so I would like to ask what would be the best/correct way to only Seed some data when the Tenant is being created.

The scenario is something like this:

  • The Host admin will create a new tenant for Client Z
  • When creating a new Tenant you would like to pre-populate some data (let's say Countries, Currencies, Post Codes, etc.)

In the method:

protected override void Seed(EntityFramework.WebPropterDbContext context)

located in the Configuration.cs method we can add code on the following path:

else
            {
                //You can add seed for tenant databases using Tenant property...
            }

So repeating the initial question, what would be the best/correct way to only seed some data when the Tenant is being created in Production and to seed all the data in the development environment.

Something like:

// Run all default data
SeedTenantSetupData();
if (isDevelopmentEnvironment){
  SeedDevelopmentData();
}

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

    Hi,

    I think your solution is right. Nothing comes to my mind as an alternative solution right now :)

  • User Avatar
    0
    hugol created

    I will share my logic and how I will implement it.

    Objectives and Restrictions:

    • Seed All the Data in Development mode
    • When the Host admin is creating a new tenant in PRODUCTION, the new tenant should have some pre populate management data.
    • The Production DBs have to be update by a DBA. The process is: they review the scripts, backup the Prod DBs, Copy them to a Pre-PROD env, Specific Users Test the new version and then DBA will run the script against all PROD DBs (host and Tenants) Developers don't have access to the production DBs for security and data protection.

    Consideration that I also used to make a decision (for now)

    • When I'm in Debug mode my connection strings are aimed to my DEV environment.
    • When deploying to the server I will do it in release mode.

    Decisions

    • I decided to use the DebugHelper
    • I will generate a database script to give to the DBA using the "Update-Database –Script"
    • The code:
    if (Tenant == null)
                {
                    // Host seed
                    // Code removed from post for simplicity
                }
                else
                {
                    InitialManagementDataBuilder(); // Countries, Post Codes, 
    
                    if (DebugHelper.IsDebug)
                    {
                        new CompaniesBuilder(context).Create();
                        new OtherEntity1Builder(context).Create();
                        new OtherEntity2Builder(context).Create();
                        ....
                    }
    

    Cheers and Thanks for the replying. HugoL