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

Deploy Multi-Tenant Web App to Azure Elastic Database Pool

作者-2
Ryan Lewis, Ibrahim Khalil
8 月 2, 2024

This post will explore how to deploy a multi-tenant web application to Microsoft Azure and use an Elastic database pool to manage per-tenant databases.

Database per-tenant (multi-db) architecture is not the only way to achieve multi-tenancy. It’s possible to design a multi-tenant application that stores all tenant data in one shared database. Each approach offers its own advantages and disadvantages and it’s up to you to decide which is the right strategy in your case.. 

使用 Nano ASP.NET Boilerplate CLI 工具, you can instantly generate new multi-tenant web applications with single-database 或 per-tenant database multi-tenancy. There’s also a single-tenant deployment option if you don’t need multi-tenant features.

In this guide, we’ll generate an app with per-tenant (multidb) multi-tenant architecture and deploy it to Azure. We’ll extend the code to work with the Azure SDK and set up Azure resources so that our app can create new databases programmatically. All this will be done by adding just a few lines of code to the default Nano multi-db template code.

If you don’t have the Nano 模板 or are coming from the blog tutorials part 1 和 part 2 about multi-tenancy, you can still follow along. The code covered is quite similar. Download the GitHub to follow along:

Download the finished version of Part 2 Completed: Simple Multi-Tenant App with Per-Tenant DB

Or if you prefer, the finished version of that: Part 3 Completed: Multi-Tenant App with Azure Elastic DB

You can grab the postman collection for that simple multi-tenant app here.

Before we get started, let’s take a look at why a database per tenant architecture might be a favorable option.

When building a Software as a Service (SaaS) application, opting for a per-tenant database approach can provide several significant advantages. Firstly, it enhances data isolation and security, as each tenant’s data is stored separately. This minimizes the risk of data breaches and simplifies compliance with data protection regulations.

A per-tenant database allows for more granular performance tuning and resource allocation. You can optimize the database settings based on the specific needs and usage patterns of each tenant, leading to better overall application performance. Elastic database pools handle most performance concerns automatically.

Having tenant databases separate facilitates easier data backup and recovery. In the event of data corruption or loss, only the affected tenant’s database needs to be restored, reducing downtime and impact on other tenants.

A per-tenant database architecture can also simplify scaling. As your SaaS application grows, you can distribute the load more effectively by scaling databases independently, ensuring consistent performance across all tenants. Azure elastic database pools provide nice functionality in this department.

So if you are willing tolerate a bit more complexity, a per-tenant database strategy can offer quite a few advantages when building SaaS applications.

Either from the command line with dotnet new nano or by creating a new project in Visual Studio, create a new multi-tenant application with per-tenant databases. Leave the multi-tenancy option to default (multidb). 

dotnet new nano -n NanoAzure

将设置用于本地的默认连接字符串 SQL Server 实例名称与数据库名称相同,且与项目名称一致。您可以在 appsettings.json

What needs to change?

At this point, we have a multi-tenant app that will create new databases for new tenants on a local SQL server host. There’s just one issue… this only works locally.

As soon as we publish our code to a cloud platform like Azure, we’ll get errors whenever we try to create a new tenant with an isolated database. There’s no code that communicates with Azure, directing it to create a new database. Luckily though, once we install the necessary Azure SDK NuGet packages, doing so is surprisingly easy.

There are some arrangements to be made in the Azure platform though before we get coding.

We need to have an Azure Elastic Database pool. The procedure to create one is outlined in the next steps, but first lets see why elastic database pools are so useful in a multi-database multi-tenant scenario.

How Elastic Pools Can Help (Our Solution)

While the per-tenant database approach offers numerous benefits, it also introduces some significant challenges, particularly in terms of cost and management. As the number of tenants grows, so does the number of databases. This can lead to increased costs for database storage and management, as well as potential performance issues. Managing a large number of databases can become complex and time-consuming, requiring additional resources and potentially leading to inefficiencies.

Azure offers a solution to these challenges through Elastic Pools. Elastic Pools allow you to manage and scale multiple databases that have varying and unpredictable usage patterns. Here’s how Elastic Pools can help manage costs and enhance performance:

  1. Cost Efficiency: Elastic Pools provide a cost-effective way to manage multiple databases by sharing resources among them. Instead of provisioning each database individually, you can allocate a shared pool of resources (e.g., DTUs or vCores) to be used by all databases within the pool. This can significantly reduce costs, especially for databases with intermittent or unpredictable workloads.
  2. Performance Optimization: By sharing resources, Elastic Pools ensures that databases get the necessary resources when they need them. This dynamic allocation of resources helps in maintaining consistent performance across all databases, even under fluctuating workloads. Databases in the pool can automatically scale within the allocated resources, ensuring optimal performance.
  3. Simplified Management: Elastic Pools simplify the management of multiple databases by centralizing resource allocation and monitoring. Administrators can easily manage and monitor the performance and resource usage of all databases within the pool from a single interface. This reduces the complexity and overhead associated with managing a large number of individual databases.
  4. Scalability: As your application grows, Elastic Pools make it easier to scale your database infrastructure. You can adjust the size of the pool or add more resources as needed, without having to manage each database individually. This ensures that your application can scale efficiently to accommodate more tenants.

By leveraging Elastic Pools, you can effectively manage the costs and performance challenges associated with a per-tenant database architecture, ensuring a scalable and efficient solution for your SaaS application.

You will need to have an Azure account with a Hosting plan for the rest of the steps.

In the Azure portal resource explorer, select the Database Server that hosts SQL Databases, and select New Elastic Pool.

Azure’s default Compute + Storage settings are ambitious, so be sure to adjust the pool performance to suit your preference and budget. The basic service tier with the lowest DTU setting will be fine for this learning project. The cost is about $70 for a pool that can support up to 100 databases. Compare that to about $5 for a basic 5-DTU single database.

Give your Elastic Pool a name and wait for the creation of the pool to complete. You can access it from the resource explorer when the process is finished. 

Within your new SQL elastic pool, you can create a new database to serve as the primary database for your new App Service. Create a database with a new name.

There are several ways you can publish an application to Azure, but we’ll do this quickly using Visual Studio 2022. Be sure to sign in to your Azure account.

Publish Profile

To create a publish profile in Visual Studio 2022, right-click NanoAzure.WebApi in the solution explorer. From the context menu, choose Publish. 

选择 Azure for the target and choose Azure App Service. We’ll create a new App Service for this project so click Create New and give it a name. 

Select your hosting plan and resource group. 创建, then select the new App Service and click Finish. Now we have a new Publish Profile.

Connect an Azure SQL Database to the application

In the publish profile, go to the Service Dependencies section, click on the menu, and select Connect. In the connection window, choose Azure SQL Database and click 下一页. This will take you to a list of all the available databases in your Azure subscription.

Select the new database we created in the elastic pool. Leave the database connection string name as is and provide the connection username and password. Save the connection string in the Azure App Settings (default) and click finish. This will configure your App Service application to use the new database.

Publishing the Application to Azure

Now we can publish our application by clicking on Publish.

Visual Studio will build the project and then publish it to Azure. When that finishes, we have now deployed our application on Azure and are using a database within an elastic pool.

The next thing we need to do is to give this App Service the permissions necessary to create new databases in our Azure subscription. We can do that by adding a role assignment as shown in the next step.

Within the Azure portal, you can move databases into and out of an elastic pool. For example, if you had already published your application to an existing database, you can move that database to the elastic pool.

In the next key step, we’re going to grant our application the permission to create a new database in the specified elastic pool within our resource group. To do that, we need to add a new role assignment. 

Start by accessing the application service from the resource explorer. Then, go to Settings and select Identity. 在 System Assigned tab, click on the Azure role assignments button. 

Next, click on Add role assignment. We need to grant the App Service enough scope and capability to be able to create databases within our Azure service. For this example we’ll choose Resource Group 用于 Scope, and select the group that contains our elastic pool and database server. You’ll need to select the Azure subscription, set Contributor for the role and save your changes.

It may take a few minutes for the new role assignment to appear on the Azure role assignment page.

At this point, the Azure infrastructure is set up to support multiple databases in an elastic pool, and our app has the role authority necessary to manage database resources within our Azure subscription.

It’s time now to get back to our solution to refactor our code. We’ll start by adding in a few new configuration variables in the appsettings.json related to our Azure service. Let’s return to Visual Studio to do this.

Edit the Application Configuration file

Locate the application configuration file appsettings.json 在 WebApi project and add these Azure configurations that we can access later in the code to authenticate our application to Azure.

"Azure": { 
"ElasticPoolName": "Your_Elastic_Pool_Name", 
"ResourceGroupName": "Your_Resource_Name",
"SqlServerName": "Your_SqlServer_Name", 
"SubscriptionId": "Your_Azure_Subscription_Name"
}

Add Azure Resource Management SDK to the Infrastructure project

Installing the Azure SDK packages will allow us to interact with Azure resources programmatically. We need to add the following NuGet packages to the infrastructure project in our solution. Right-click on the infrastructure project and select manage NuGet packages, or you can install these via the command line

From .NET CLI

dotnet add package Azure.ResourceManager 
dotnet add package Azure.ResourceManager.Sql

From Package Manager

NuGet\Install-Package Azure.ResourceManager
NuGet\Install-Package Azure.ResourceManager.Sql

With the Azure SDK NuGet packages added, the solution now can use the Azure commands. We’ll refactor the tenant management service so that when we create a new tenant, our application will also create a new database for the tenant within the Azure SQL Elastic Pool. 

Import namespaces

打开 租户管理服务. Start by importing the various Azure using statements we need at the top:

使用 Azure.Core;
使用 Azure.Identity;
使用 Azure.ResourceManager;
使用 Azure.ResourceManager.Sql;

Check for environment

Since it would be nice to have the create tenant database process work both locally and on production, we’ll create a conditional check for the environment. By injecting the IWebHostEnvironment Interface in the 租户管理服务, we can check the web hosting environment of our application.

   私有 只读 IWebHostEnvironment _environment;

   public 租户管理服务(BaseDbContext baseDbContext, IConfiguration 配置, IServiceProvider serviceProvider, UserManager<ApplicationUser> userManager, IWebHostEnvironment environment)
   {
       _baseDbContext = baseDbContext;
       _configuration = 配置;
       _serviceProvider = serviceProvider;
       _userManager = userManager;
       _environment = environment; // add the environment
   }

Refactor the SaveTenant Method

Finally, we can add the code that creates new databases in the Azure cloud.

在 Save Tenant method, after checking if the request.hasIsolatedDatabase value is true, we’ll add our new code. Note that we need to use a fully qualified name whenever we use Response since the Azure SDK also contains a class named Response.

If the environment is 发展, the existing local SQL Server code will run. If the environment is production, we’ll initialize the Azure resources and create a new database in the elastic pool.

The complete code is as follows:

public async 任务<应用.Common.Wrapper.回复<租户>> SaveTenant(CreateTenantRequest request)
{
    字符串 organizationSlug = NanoHelpers.ToUrlSlug(请求.Id);
    bool tenantExists = 等待 _baseDbContext.租户.AnyAsync(x => x.Id == organizationSlug);

    如果 (tenantExists) // 检查租户是否存在
    {
        返回 应用.Common.Wrapper.回复<租户>.Fail("Tenant already exists");
    }

    字符串 connectionString = _configuration.GetConnectionString("默认连接");
    SqlConnectionStringBuilder builder = 新(connectionString);
    字符串 mainDatabaseName = 构建者.InitialCatalog; // retrieve the database name
    字符串 tenantDbName = mainDatabaseName + "-" + organizationSlug;
    构建者.InitialCatalog = tenantDbName; // set new database name
    字符串 modifiedConnectionString = 构建者.ConnectionString; // create new connection string

    Tenant tenant = 新() // create a new tenant entity
    {
        Id = 请求.Id,
        姓名 = 请求.姓名,
        ConnectionString = 请求.HasIsolatedDatabase ? modifiedConnectionString : null,
        CreatedOn = DateTime.UtcNow,
        IsActive = true
    };

    试一试
    {
        _ = 等待 _baseDbContext.租户.AddAsync(租户);

        ApplicationUser user = 新()
        {
            UserName = 请求.AdminEmail + "." + 租户.Id, // username must be unique: Email + TenantId
            FirstName = "Default",
            LastName = "Admin",
            Email = 请求.AdminEmail,
            EmailConfirmed = true,
            租户ID = 租户.Id
        };

        IdentityResult
            结果 = 等待 _userManager.CreateAsync(user,
                请求.Password); // create a default admin user upon tenant creation
        如果 (结果.Succeeded) // success -- add to admin role
        {
            如果 (请求.HasIsolatedDatabase) // create new db and run any appDb migrations (this section can be removed if you don't plan to use per-tenant databases)
            {
                试一试
                {
                    // Our Refactoring Start Here
                    IServiceScope scopeTenant = _serviceProvider.CreateScope();
                    ApplicationDbContext dbContext =
                        scopeTenant.ServiceProvider.GetRequiredService<ApplicationDbContext>();
                    dbContext.Database.SetConnectionString(modifiedConnectionString);

                    如果 (_environment.IsDevelopment()) // Create a new database in the local SQL Server instance when app is running in development mode
                    {
                        如果 ((等待 dbContext.Database.GetPendingMigrationsAsync()).Any())
                        {
                            控制台.ForegroundColor = ConsoleColor.蓝色;
                            控制台.WriteLine(
                                "$”Applying ApplicationDB Migrations for New '{租户.Id}' tenant.");
                            控制台.ResetColor();
                            等待 dbContext.Database.MigrateAsync();
                        }
                    }

                    else // Create a new database in the Elastic Pool in Azure SQL Server when app is running in production mode
                    {
                        // Initializing needed  Azure Resources
                        IConfigurationSection azureConfig = _configuration.GetSection("Azure");
                        DefaultAzureCredential credential = 新();
                        ArmClient armClient = 新(credential, azureConfig["SubscriptionId"]);
                        Azure.回复<SqlServerResource> sqlServer = 等待 armClient.GetResourceGroupResource(
                                新 ResourceIdentifier(
                                    "$”/subscriptions/{azureConfig["SubscriptionId"]}/resourceGroups/{azureConfig["ResourceGroupName"]}"))
                            .GetAsync()
                            .Result
                            .Value.GetSqlServerAsync(azureConfig["SqlServerName"]);

                        // Create a new database in the Elastic Pool
                        ArmOperation<SqlDatabaseResource> databaseResponse = 等待 sqlServer.Value
                            .GetSqlDatabases().CreateOrUpdateAsync(WaitUntil.Completed,
                                tenantDbName,
                                新 SqlDatabaseData(新 AzureLocation(sqlServer.Value.数据.Location))
                                {
                                    ElasticPoolId =
                                        (等待 sqlServer.Value.GetElasticPoolAsync(
                                            azureConfig["ElasticPoolName"]))
                                        .Value.数据.Id
                                });

                        // apply migrations to the new database
                        等待 dbContext.Database.MigrateAsync();

                        如果 (databaseResponse.Value.数据.Id == null)
                        {
                            返回 应用.Common.Wrapper.回复<租户>.Fail(
                                "$”Error creating isolated database for tenant {租户.Id}");
                        }
                    }
                }
                catch (异常 ex)
                {
                    throw简体中文(大陆) 新 异常(ex简体中文(大陆).消息);
                }
            }


            _ = 等待 _userManager.AddToRoleAsync(user, "管理员"); // identity save changes to baseDb
            _ = 等待 _baseDbContext.SaveChangesAsync(); // tenant save changes to baseDb
            返回 应用.Common.Wrapper.回复<租户>.Success(租户);
        }
        else // fail -- return error messages
        {
            列表<字符串> messages = 新();
            foreach (IdentityError error 在 结果.Errors)
            {
                messages.添加(error.描述);
            }

            返回 应用.Common.Wrapper.回复<租户>.Fail(messages);
        }
    }
    catch (异常 ex)
    {
        throw简体中文(大陆) 新 异常(ex简体中文(大陆).消息);
    }
}

The code to programmatically create Azure database resources will first look at our appconfig.json file to get any necessary variables. Using our credentials, we get a sql server object to work with from Azure. 

In the next line, we’ll use this sql server object to create a new database for the tenant. We’ll await this operation and the next one where we apply all pending migrations.

Creating a new database on Azure will take several seconds, so you might want to provide some progress indication for the user. 

With this code in place our application will now create new tenant databases in our Azure elastic pool!

Whenever we create new migrations, the 添加并迁移租户数据库 extension will loop through all the tenant connection strings and apply the changes for each tenant when the application starts up (deployments).

Now let’s publish our changes to Azure and test creating a new tenant with Postman.

请记得设置 hasIsolatedDatabase 到 true. From the API response, we can see that we have successfully created a new tenant with an isolated database in our Azure SQL Elastic Pool.

该连接字符串将用于新租户的数据库

As you can see it’s not so difficult to manage multiple databases on an Azure elastic database pool. With only a few additions to the Nano 模板 code, our app now creates new tenant databases programmatically in an Azure elastic database pool.

发表回复

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

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