Back to Devexpress

Use EF Core Migrations to Update the Database Schema in Multi-Tenant Applications

expressappframework-405376-multitenancy-ef-core-migrations-in-multi-tenant-application.md

latest4.0 KB
Original Source

Use EF Core Migrations to Update the Database Schema in Multi-Tenant Applications

  • Oct 27, 2025
  • 4 minutes to read

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:

  • EF Core migrations do not update the database data. This means that the logic implemented in ModuleUpdater is not executed during a migration.
  • The first migration must be applied before the initial application launch to create the database structure.

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

Apply Migrations to Tenant Databases

When using EF Core migrations for tenant databases, the approach is similar to regular databases, but requires specific considerations for multi-tenant applications:

  • You need to explicitly specify the connection string for each tenant’s database when applying a migration.
  • Any changes to the business model require you to apply a migration to every tenant database.
  • For a new tenant, its database must be created using migrations strictly before the first login.

The following steps outline the recommended sequence for using migrations for tenant databases in a multi-tenant application:

  1. Once you have created the application project and before you run it, create a new migration that will create empty databases for tenants:

  2. 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:

  3. Run the application and log into the host interface to create tenant records in the host database.

  4. Optional. Log into tenant databases to populate them with initial data.

  5. Modify the structure of business objects.

  6. Create a new migration:

  7. Apply the new migration to every tenant database:

  8. Repeat steps 4-7 as functionality is added to the application.

Apply Migrations to the Host Database

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:

  1. Since an XAF application does not contain DbContext for the host database, you need to create a separate DbContext specifically for it:

  2. In the multi-tenancy configuration code, call the WithHostDbContext<TDbContext> method to assign the new DbContext to the host database:

  3. Once you have created the application project and before you run it, create a new migration that will create the empty host database:

  4. Before the first run of the application, apply the new migration to create the host database.

  5. Modify the structure of business objects in the host database.

  6. Create a new migration.

  7. Apply the new migration to the host database.

  8. Repeat steps 5-7 as functionality is added to the application.