Nano ASP.NET SaaS 模板
管理员凭据(所有租户):admin@email.com / Password123!
样本数据每小时重置一次

Build a Multi-Tenant App With Multiple Databases – Free Guide (Part 2)

瑞安·刘易斯
9 月 13, 2023

In the last article, we learned the basics of building a multi-tenant application with ASP.NET 和 Entity Framework. In this article, we will build on that same project and refactor the solution to use a per-tenant database 方法。.

If you haven’t read the previous article on Building Multi-Tenant Applications with Entity Framework and ASP.NET, please check that out here. If you don’t already have it, download the finished GitHub project of that article because that will serve as the starting point for this article.

Download the finished version of Part 1: Single-Database Multi-Tenant App here from GitHub

The completed version of the multi-tenant solution we will build in this article is also available as a finished GitHub repository.

Download the finished version of this tutorial Part 2: Multiple-Database Multi-Tenant App here from GitHub

Download the Postman collection for testing 这里.

To support multiple databases, we will need to make a few critical changes:

  • Save a connection string per each tenant
  • Add an OnConfiguring method to our database context model to allow for using a different connection string per each request.
  • Add a tenant service & controller to allow creating new tenants
  • Run migrations when creating a new tenant
  • During startup, run any pending migrations on tenant databases

To begin, let’s start by adding a new string field to the 租户 table and call it ConnectionString. This is where we will specify the connection string for the tenant-specific database or choose to leave null and have the tenant use the default shared database. Make sure to allow null values on the ConnectionString property by adding a question mark.

public 类 租户
{
    [关键]
    [数据库生成(数据库生成的选项.无)]
    public 字符串 Id { get; set; }
    public 字符串 姓名 { get; set; }
    public 字符串? ConnectionString { get; set; }

}

Next we should update our CurrentTenantService class and interface to include this new ConnectionString field so that the ConnectionString property is always available.

在 SetTenant method, we will retrieve the connection string from the database and set the property at the same time that we set the tenant ID.

With that out of the way, we’ll update the ApplicationDbContext class to use the connection string when there is one in its 关于配置 override method, which fires on every request.

In the last tutorial, we added two key overrides 在 ApplicationDBContext to get our multi-tenant application working. The first was the OnModelCreating override where we added a query filter to select only those records which had matching a tenant Id. The second was the SaveChanges override where we saved the current tenant Id to any entity that implemented the IMustHaveTenant interface.

We will now add a third override to tell entity framework to use a different connection string whenever there’s one specified by the current tenant. The override we will add is the 关于配置 method, which fires on every request.

 public 类 ApplicationDbContext : DbContext
 {
     私有 只读 ICurrentTenantService _currentTenantService;
     public 字符串 当前租户ID { get; set; }
     public 字符串 CurrentTenantConnectionString { get; set; }


     // 构造函数 
     public ApplicationDbContext(ICurrentTenantService currentTenantService, DbContextOptions<ApplicationDbContext> 选项) : 基数(选项)
     {
         _currentTenantService = currentTenantService;
         当前租户ID = _currentTenantService.租户ID;
         CurrentTenantConnectionString = _currentTenantService.ConnectionString;

     }

     // Application DbSets -- create for entity types to be applied to all databases
     public DbSet<产品> 产品 { get; set; }

     // On Model Creating - multitenancy query filter, fires once on app start
     受保护的 覆盖 void OnModelCreating(ModelBuilder 构建器)
     {         
         构建者.实体<产品>().HasQueryFilter(a => a.租户ID == 当前租户ID); 
     }

     // On Configuring -- dynamic connection string, fires on every request
     受保护的 覆盖 void 关于配置(DbContextOptionsBuilder optionsBuilder)
     {
         字符串 tenantConnectionString = CurrentTenantConnectionString;
         如果 (!字符串.IsNullOrEmpty(tenantConnectionString)) // use tenant db if one is specified
         {
             _ = optionsBuilder.UseSqlServer(tenantConnectionString);
         }
     }
     
     // 保存更改时——将租户 ID 写入表中
     public 覆盖 int SaveChanges()
     {        
         foreach (var 条目 在 ChangeTracker.条目<IMustHaveTenant>().ToList()) 
         {
             开关 (条目.州)
             {
                 案例 EntityState.已添加:
                 案例 EntityState.已修改:
                     条目.实体.租户ID = 当前租户ID; 
                     break;
             }
         }
         var 结果 = 基数.SaveChanges();
         返回 结果;
     }
 }

The code in the 关于配置 override checks for any tenant specific connection string for the current tenant and when there is one, passes that connection string as an option optionsBuilder. Doing this will tell entity framework to use this connection string for the current request. If the current tenant connection string is null, the optionsBuilder code isn’t triggered and the request will use the default connection string like it normally would.

In a multiple database setup, we need to consider that there are two types of databases. One being the main ‘central’ shared database that stores the tenant table itself, along with any other configuration-related tables that we may add in the future like subscription, billing, or even identity. And then there are all the other databases, which are per-tenant and should not contain those central configuration tables. The tenant-specific databases should only contain application-related tables, like 产品 and any more that we add.

To achieve this, we will need manage two sets of migrations: one set for entities that are exclusive to the central database, and another set for all the other tables which can be found across all the databases.

Let’s begin by removing the 移民 and database snapshot we created in the previous tutorial (there should only be one called initial).  We should also remove the Tenants DbSet from the ApplicationDbContext because we don’t want that table to be generated in the tenant-specific databases.

If we were being strict about every tenant having their own database, we could actually remove the tenant query filters and tenant ID columns. Its better to leave it though because it will allow for tenants to share the same database and still retain data isolation. This is what you would call a 混合型 multi-tenant implementation.

In our single-database multi-tenant setup, we used the TenantDbContext purely for looking up tenant information when a request comes in. It was essentially a read-only context and had no responsibilities in terms of migrations. In this multi-database setup, we’ll still be using it as a lookup context for incoming request, but we’ll also use it to manage migrations on tables specific to the main central database.  The ApplicationDbContext will manage migrations on tables found across every tenant database.

打开 包管理器控制台 and create an initial migration for the central database. For that we will need to target the TenantDbContext. To keep things organized, we can also specify an output directory for migrations to keep the two groups separate with the -o switch. Here is the command:

add-migration Initial -context TenantDbContext -o Migrations/TenantDb

Lets apply the migration by typing the following:

update-database -context TenantDbContext 

Now if we go explore our databases, can see that one was created and it contains one table for tenants. If we expand the migrations folder in the solution explorer, we should see there is a new subfolder called TenantDb and that contains our migration. If that’s the case for you, then you’ve done everything correctly up to this point.

Next, we’ll create another migration this time for the ApplicationDbContext. Back in 包管理器控制台 type the following command, this time notice we specify a new context and a new output directory:

add-migration InitialApp -context ApplicationDbContext -o Migrations/AppDb

Let’s apply the migration by typing the following:

update-database -context ApplicationDbContext 

If we check the database now, we’ll see that the products table has been created. In the solution explorer, there is another folder in migrations with the new migration we just added.

在 single-database setup from the previous tutorial, we never actually made a service or controller to handle creation of new tenants. In that simplistic setup, we just added the tenants manually in the table itself. With a multiple database setup however, that will not suffice because we will need to run some automation when we create new tenants. Every time a new tenant is created, we’ll need to generate a connection string, spin up a new database, and apply any pending migrations.

Start by creating a new folder in 服务 称为 TenantService and add an interface called ITenantService. In this interface define one method called CreateTenant. In a real application, there should be methods for retrieving lists, and updating tenants. For brevity, we will only worry about creating tenants in this tutorial.

public 接口 ITenantService
{
    租户 CreateTenant(CreateTenantRequest request);
}

创建一个新的 DTOs folder within the TenantService folder and call it CreateTenantRequest. It will contain an ID 和 姓名 property like the 租户 entity itself and also a Boolean field called Isolated to indicate if this tenant should have its own isolated database.

public 类 CreateTenantRequest
{
    public 字符串 Id { get; set; }
    public 字符串 姓名 { get; set; }
    public bool Isolated { get; set; }
}

Finally let’s create the service itself, called TenantService.

public 类 TenantService : ITenantService
{

    私有 只读 TenantDbContext _context; // 数据库上下文
    私有 只读 IConfiguration _configuration;
    私有 只读 IServiceProvider _serviceProvider;

    public TenantService(TenantDbContext 上下文, IConfiguration configuration, IServiceProvider serviceProvider)
    {
        _context = 上下文;
        _configuration = 配置;
        _serviceProvider = serviceProvider;
    }

    public 租户 CreateTenant(CreateTenantRequest request)
    {

        字符串 newConnectionString = null;
        如果 (请求.Isolated == true)
        {
            // generate a connection string for new tenant database
            字符串 dbName = "multiTenantAppDb-" + 请求.Id;
            字符串 defaultConnectionString = _configuration.GetConnectionString("默认连接");
            newConnectionString = defaultConnectionString.替换("multiTenantAppDb", dbName);

            // create a new tenant database and bring current with any pending migrations from ApplicationDbContext
            试一试
            {
                使用 IServiceScope scopeTenant = _serviceProvider.CreateScope();
                ApplicationDbContext dbContext =   scopeTenant.ServiceProvider.GetRequiredService<ApplicationDbContext>();
                dbContext.Database.SetConnectionString(newConnectionString);
                如果 (dbContext.Database.GetPendingMigrations().Any())
                {
                    控制台.ForegroundColor = ConsoleColor.蓝色;
                    控制台.WriteLine("$”Applying ApplicationDB Migrations for New '{请求.Id}' tenant.");
                    控制台.ResetColor();
                    dbContext.Database.Migrate();
                }
            }
            catch (异常 ex)
            {
                throw简体中文(大陆) 新 异常(ex简体中文(大陆).消息);
            }
        }


        Tenant tenant = 新() // create a new tenant entity
        {
            Id = 请求.Id,
            姓名 = 请求.姓名,
            ConnectionString = newConnectionString,
        };

        _context.添加(租户);
        _context.SaveChanges();

        返回 租户;
    }
}

In this service, we’ll use the TenantDbContext to interact with tenant data since its data that is managed only in the central database. The CreateTenant method takes the DTO as a request and in the case of an isolated tenant, performs several additional steps.

The first step is to generate a new connection string. We could just hard code a base connection string, but a slightly more elegant solution is to use the IConfiguration interface, which allows us to read fields from appsettings.json. We read the 默认连接 来自 appsettings.json and simply add a dash with the Id of the tenant to the central database name. In a real application, you might want to ensure that no whitespace or illegal characters are present in the Id of the tenant before proceeding further.

The next step is critical. Using the help of the IServiceProvider interface, we pull down a new scoped instance of the ApplicationDbContext from the service provider. With this instance, we set the new connection string on it and then check for any pending migrations. Of course there will be pending migrations because there isn’t even a database created yet. The framework will detect that and apply all pending ApplicationDbContext migrations.

Isolated database or not, the final step of the create tenant method is to save the new Tenant to the main database.

Lastly, in Program.cs we’ll need to add the Tenant service to the service container as a transient service so it can be used throughout our application. Add the following line after the product service we created in the previous tutorial.

构建者.服务.AddTransient<ITenantService, TenantService>();

To make use of the new tenant service and interact with it via REST API calls, create a new API 控制器 在 控制器 名为 TenantsController. We’ll inject the tenantService in the constructor and use it in the Post method as follows:

[路线("api/[控制器]")]
[ApiController]
public 类 TenantsController : ControllerBase
{
    私有 只读 ITenantService _tenantService;

    public TenantsController(ITenantService tenantService)
    {
        _tenantService = tenantService;
    }

    public ITenantService TenantService { get; }

    // Create a new tenant
    [HttpPost]
    public IActionResult 帖子(CreateTenantRequest request)
    {
        var 结果 = _tenantService.CreateTenant(请求);
        返回 好的(结果);
    }
}

With that out of the way, run the application and we can open up Postman to test everything out.

Make a new 帖子 API call with the following JSON in the body. The 姓名 of the tenant can be however you want but the Id cannot contain any special characters or whitespace. The Id remember is what will be appended to the database name to create the new tenant-specific database. Set the Isolated boolean to true so that a new database is created when you send the request.

Here is a postman collection you can use to make your life easier.

Once you click send, you should receive a 200 OK status response and a new database should appear when you refresh your database explorer! Create a few more tenants both isolated and shared to see what happens.

The query filters and dynamic connection string should be working too. Try creating some sample products to see what happens. Remember to change the tenant ID in the header when sending requests. New products will be saved to their tenant databases when a connection string is specified. Now obviously this is in no way secure since anyone can just change the request header and access another tenant. To secure the application you would need to integrate ASP Identity and store/read the tenant key in a token. The Nano ASP.NET 模板 has that all taken care of already along with a lot of other great features and will definitely save you a lot of time messing with that.

Alas we have multi-tenant behavior implemented in a per-tenant database model! But we can’t call it a day just yet. We need to consider what happens when we create new migrations. If we add a new field to Product and run a migration, that migration won’t be applied to all the tenant databases, only the central one. That is a problem that we will address in the following section.

Running the 更新数据库 command will apply migrations on only the one database specified in the default connection string found in appsettings.json. What we need is to loop through each tenant, and apply migrations for every database found – not just the main one. Luckily, there is a way to programmatically apply migrations instead of having to type the command into PMC using the .migrate() method. We’ll rely on this method to achieve the goal.

In summary what we need to do is:

  • Tap into the app startup process using a service extension
  • Apply any migrations to the main database (TenantDbContext)
  • Read the Tenant table
  • Loop through each tenant
  • When a tenant has a connection string, apply any ApplicationDbContext migrations for this database instance

Start by creating a new folder in the solution explorer called 扩展. What we are creating here is an extension method to the app configuration process. I’ve named this static class MultipleDatabaseExtensions and it has one method 添加并迁移租户数据库. The code is as follows:

public static 类 MultipleDatabaseExtensions
{
    public static IServiceCollection 添加并迁移租户数据库(this IServiceCollection services,   IConfiguration configuration)
    {

        // Tenant Db Context (reference context) - get a list of tenants
        使用 IServiceScope scopeTenant = services.BuildServiceProvider().CreateScope();
        TenantDbContext tenantDbContext = scopeTenant.ServiceProvider.GetRequiredService<TenantDbContext>();

        如果 (tenantDbContext.Database.GetPendingMigrations().Any())
        {
            控制台.ForegroundColor = ConsoleColor.蓝色;
            控制台.WriteLine("Applying BaseDb Migrations.");
            控制台.ResetColor();
            tenantDbContext.Database.Migrate(); // apply migrations on baseDbContext
        }


        列表<租户> tenantsInDb = tenantDbContext.租户.ToList();

        字符串 defaultConnectionString = 配置.GetConnectionString("默认连接"); // read default connection string from appsettings.json

        foreach (Tenant tenant 在 tenantsInDb) // loop through all tenants, apply migrations on applicationDbContext
        {
            字符串 connectionString = 字符串.IsNullOrEmpty(租户.ConnectionString) ? defaultConnectionString : 租户.ConnectionString;

            // Application Db Context (app - per tenant)
            使用 IServiceScope scopeApplication = services.BuildServiceProvider().CreateScope();
            ApplicationDbContext dbContext = scopeApplication.ServiceProvider.GetRequiredService<ApplicationDbContext>();
            dbContext.Database.SetConnectionString(connectionString);
            如果 (dbContext.Database.GetPendingMigrations().Any())
            {
                控制台.ForegroundColor = ConsoleColor.蓝色;
                控制台.WriteLine("$”Applying Migrations for '{租户.Id}' tenant.");
                控制台.ResetColor();
                dbContext.Database.Migrate();
            }
        }

        返回 services;
    }

}

This class is going to take care of migrations for both contexts, TenantDbContext 和 ApplicationDbContext. First it will create a scoped instance of the TenantDbContext on the main database. We know it’s the main database because we aren’t specifying any connection string options and it will therefore use the default one. If there any TenantDbContext migrations pending, they will get applied.

Next, we read a list of Tenants using this same context instance. We loop through each tenant in the list and if there is a connection string present, we use it. Otherwise, in a case of null, we set the connection string to the default database. Any pending ApplicationDbContext migrations are then applied to the target database instance.

Finally, we need to hook this into the app configuration process back in program.cs. Right after the code that adds database contexts, add this line. Now this extension will run whenever the app starts.

构建者.服务.添加并迁移租户数据库(构建者.配置);

This is how the Program.cs class should look at this point

使用 微软.EntityFrameworkCore;
使用 multiTenantApp.扩展;
使用 multiTenantApp.中间件;
使用 multiTenantApp.模型;
使用 multiTenantApp.服务;
使用 multiTenantApp.服务.产品与服务;
使用 multiTenantApp.服务.TenantService;

var 构建者 = WebApplication.CreateBuilder(args);

// Add services to the container.
构建者.服务.AddControllers();
构建者.服务.AddEndpointsApiExplorer();

// Current tenant service with scoped lifetime (created per each request)
构建者.服务.AddScoped<ICurrentTenantService, CurrentTenantService>();

// 添加一个带有配置的数据库服务——连接字符串从 appsettings.json 中读取
构建者.服务.AddDbContext<ApplicationDbContext>(选项 => 选项.UseSqlServer(构建者.配置.GetConnectionString("默认连接")));
构建者.服务.AddDbContext<TenantDbContext>(选项 => 选项.UseSqlServer(构建者.配置.GetConnectionString("默认连接")));
构建者.服务.添加并迁移租户数据库(构建者.配置);

// Product CRUD service with transient lifetime
构建者.服务.AddTransient<ITenantService, TenantService>();
构建者.服务.AddTransient<IProductService, 产品与服务>();

var app = 构建者.构建();

app.使用HTTPS重定向();
app.使用授权();
app.UseMiddleware<TenantResolver>();
app.MapControllers();

app.运行();

To test out our new extension, create a new string field in the 产品 table called 供应商. Generate a new migration with the following command, remember to specify the 上下文 以及 output folder. Since 产品 is an application table found in every database, we will use the ApplicationDbContext:

add-migration AddFieldToProduct -context ApplicationDbContext -o Migrations/AppDb

Now that our migrations are handled programmatically, all we need to do is run the application. There’s no need to run the 更新数据库 command. If you do run the 更新数据库 command, you will get a message saying that no migrations were applied. This is because before Entity Framework applies the migrations, it builds the application. During that build process, our application applies the pending migrations. As a result, by the time the command runs, no migrations are pending.

Run the app and inspect the databases. You will see that the new field has been added to our main database and all of the tenant databases as well. We now have a completely functional multiple database multi-tenant setup.

Any migrations on both the TenantDbContext 和 ApplicationDbContext will be handled programmatically by this new extension method.

We may find ourselves feeling like Ripley from the original Alien film now, having nuked the USCSS Nostromo and thinking our problems are behind us. But suddenly the alien crawls out of the wall for one final fight. And here is that scene for us:

There is one last issue and that is that you will get an error if you try to add any fields to the 租户 table. Strangely, adding new entities to the TenantDbContext or modifying any of those entities wont result in any problem. Only the 租户 table will give you an error. Try adding a new field to the Tenant entity called SubscriptionLevel and create a new migration. Remember to specify the TenantDbContext and the output directory:

add-migration AddFieldToTenant -context TenantDbContext -o Migrations/TenantDb

Now run the app to apply the migration. This is the error you will receive

This, as you could image, took hours to figure out just what in the actual F&#! was going on when I first encountered the issue.

The problem is in our 添加并迁移租户数据库 method, where we read a list of 租户 from the Tenants table. We cannot read from a table that is being modified during design time. Well, we can but we need to tell the Entity Framework scaffolding tool explicitly which connection string we want to use in design time.

Entity Framework Core provides an interface we can use to specify criteria we want to use which will override and default behavior during design time. That interface is aptly named IDesignTimeDbContextFactory and it takes the DbContext in question as a type. Create a new class in the 模型 名为 TenantDbContextFactory and follow the code example below:

public 类 TenantDbContextFactory : IDesignTimeDbContextFactory<TenantDbContext>
{
    public TenantDbContext CreateDbContext(字符串[] args) // neccessary for EF migration designer to run on this context
    {

        // Build the configuration by reading from the appsettings.json file (requires Microsoft.Extensions.Configuration.Json Nuget Package)
        IConfigurationRoot configuration = 新 ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .构建();

        // Retrieve the connection string from the configuration
        字符串 connectionString = 配置.GetConnectionString("默认连接");


        DbContextOptionsBuilder<TenantDbContext> optionsBuilder = 新();
        _ = optionsBuilder.UseSqlServer(connectionString);
        返回 新 TenantDbContext(optionsBuilder.选项);
    }
}

The critical line of code is the DbContextOptionsBuilder<TenantDbContext> part near the end where we specify the connection string. The rest of the code is just making things clean by reading the default connection string from appsettings.json to avoid hard coding it in this class. To read from configuration in this class you will need to install one Nuget package, Microsoft.Extensions.Configuration.Json.

With that class added, run the app to apply the new migration. The app should start without issue and the new field will be added to the tenant table.

Well, that was fun, wasn’t it? Now you can tell all your friends you know how to create a multi-tenant application with a database per tenant implementation and they will no doubt think that that’s awesome. They may even proceed to tell you about their new app idea and ask you to build it for them.

There are a few things to consider before embarking down the database per tenant path. One is that this setup is only complete on our local development environment. You would need to add additional code to handle creating new databases on your cloud provider’s infrastructure. Cloud providers like Azure and AWS provide .NET SDKs for this very thing.

The other concern is that managing migrations can be difficult in situations where things go wrong. Imagine a scenario though where you have 50 tenant databases and for some reason one of them throws an error. Reverting to a previous state or doing any kind of backtracking is notoriously difficult. You should plan for these kinds of issues before deciding to run multiple databases.

Consider all the pros and cons of a multiple database setup before choosing it. A multi-database setup may be more efficient when it comes to scaling but single database multi-tenant setups are much easier to manage in regards to deployment and migrations.

If you are planning to build an MVP and are looking for it to scale and have robust SaaS features, consider using the Nano ASP.NET 模板. You’ll yourself a lot of time getting your project off the ground.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

3 comments on “Build a Multi-Tenant App With Multiple Databases – Free Guide (Part 2)”

  1. Ntsikelelo说道:

    This was amazing! I am new to the world of multi-tenancy, could not have wished for a better intro.

  2. John Holliday说道:

    Execellent read. I’m using the .NET 8.0 version which has similar functionality, but does not map to the code you reference in this article. It would be great to have an updated version that matches the code.

  3. Michel说道:

    This post help me a lot. Thanks for sharing.

需要一个 ASP.NET 模板来构建您的下一个 MVP 吗?
快来看看这个 Nano ASP.NET 多租户 SaaS 模板项目,为您节省数周甚至数月的开发时间。.
了解更多