expressappframework-405376-multitenancy-ef-core-migrations-in-multi-tenant-application.md
When you use EF Core migrations in a multi-tenant application, the following points are still relevant, just as they are in a non-multi-tenant application:
ModuleUpdater is not executed during a migration.Note
Disable the XAF database update mechanism, because it can disrupt EF Core migrations. Refer to the following topic for more information: Disable Automatic Database Schema Updates
When using EF Core migrations for tenant databases, the approach is similar to regular databases, but requires specific considerations for multi-tenant applications:
The following steps outline the recommended sequence for using migrations for tenant databases in a multi-tenant application:
Once you have created the application project and before you run it, create a new migration that will create empty databases for tenants:
Before the first run of the application, apply the new migration to create a database for every tenant. The Template Kit initially creates two tenants in the application: company1.com and company2.com. In this case, the migration must be applied twice, with different connection strings for the corresponding tenants:
Run the application and log into the host interface to create tenant records in the host database.
Optional. Log into tenant databases to populate them with initial data.
Modify the structure of business objects.
Create a new migration:
Apply the new migration to every tenant database:
Repeat steps 4-7 as functionality is added to the application.
The host database is intended to store service information about tenants and the administrator user account. Usually, its data structure does not change, and applying EF Core migrations to the host database is not required.
However, it is technically possible to apply migrations to the host database. The following steps demonstrate how to apply migrations to the host database in a multi-tenant application:
Since an XAF application does not contain DbContext for the host database, you need to create a separate DbContext specifically for it:
In the multi-tenancy configuration code, call the WithHostDbContext<TDbContext> method to assign the new DbContext to the host database:
Once you have created the application project and before you run it, create a new migration that will create the empty host database:
Before the first run of the application, apply the new migration to create the host database.
Modify the structure of business objects in the host database.
Create a new migration.
Apply the new migration to the host database.
Repeat steps 5-7 as functionality is added to the application.