SharedFlat and Databases

栏目: IT技术 · 发布时间: 4年前

内容简介:This post is part of a series onThere are essentially three strategies for doing multitenancy when it comes to databases:Let’s see how we can have this with SharedFlat! First, it’s needed to say that this uses Entity Framework Core 3.1 and that there is a

Introduction

This post is part of a series on SharedFlat . Seehere the first (introduction) andhere the second (UI). This time I will be explaining how SharedFlat handles multitenant databases.

There are essentially three strategies for doing multitenancy when it comes to databases:

  1. One connection string per tenant, which effectively means a different database for each tenant

  2. One schema per tenant, meaning, one table will exist on a schema for each tenant

  3. A single database where multitenant tables have a column that is used to distinguish tenants

Let’s see how we can have this with SharedFlat! First, it’s needed to say that this uses Entity Framework Core 3.1 and that there is a TenantDbContext abstract class that we should inherit from.

Database per Tenant

Let’s start with database per tenant. In this case, it is expected that we store in the configuration (typically the appsettings.json file) one connection string for each for each tenant. For example:

{
  "ConnectionStrings": {
    "abc": "<connection string for abc>",
    "xpto": "<connection string for xpto>"
}
}

We just need, when registering the services, to use the DifferentConnectionPerTenant extension method:

services
.AddTenantService()
.AddTenantDbContextIdentification()
.DifferentConnectionPerTenant();

And SharedFlat will know what to do. If, for any reason, you wish to use a differently-named connection string, there is an overload that lets you customize this:

services
.AddTenantService()
.AddTenantDbContextIdentification()
.DifferentConnectionPerTenant(options =>
{
options.Mapping["xpto"] = "xpto2";
});

Schema per Tenant

Next is different schema per tenant. The extension method to call is DifferentSchemaPerTenant :

services
.AddTenantService()
.AddTenantDbContextIdentification()
.DifferentSchemaPerTenant();

By default, the schema will be identical to the tenant; should we need to change that, there is an overload:

services
.AddTenantService()
.AddTenantDbContextIdentification()
.DifferentSchemaPerTenant(options =>
{
options.Mapping["xpto"] = "xpto2";
});

This will set the schema on each entity that implements the ITenantEntity interface (a simple marker interface) when the TenantDbContext -derived class is initialized. To be clear, using SQL Server as an example, it will change the default (“ dbo ”) for the tenant value, so a table named “ dbo.Product ” will become “ abc.Product ”, “ xpto.Product ”, etc.

Filter by Tenant

The last technique depends on having a column on each table that we want to make multitenant, as dictated by its entity implementing ITenantEntity . The method to call is FilterByTenant , unsurprisingly:

services
.AddTenantService()
.AddTenantDbContextIdentification()
.FilterByTenant();

As is usual, the method has some defaults, like, the name of the column (“ Tenant ”), but we can change it:

services
.AddTenantService()
.AddTenantDbContextIdentification()
.FilterByTenant("TenanantCol");

Or, in the eventual case where you need to change the mapping:

services
.AddTenantService()
.AddTenantDbContextIdentification()
.FilterByTenant(options =>
{
options.Mapping["xpto"] = "xpto2";
});

Filtering by tenant means that whenever the context is querying for a multitenant-aware entity, a filter is added:

SELECT p.[ProductId], p.[Name], p.[Price]
FROM [dbo].[Product] p
WHERE p.[Tenant] = 'abc'

Conclusion

And this is it for multitenant databases. Keep tuned for more on SharedFlat, and, as always, let me hear your thoughts! SharedFlat and Databases


以上所述就是小编给大家介绍的《SharedFlat and Databases》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

千夫所指

千夫所指

乔恩·罗森 / 王岑卉 / 九州出版社 / 2016-10-1 / CNY 42.80

编辑推荐: 《乌合之众》是为了跪舔权贵?《普通心理学》实验存在重大漏洞?《引爆点》的理论都是瞎掰的?社交网络时代《1984》预言的“老大哥”是否已经变成事实? 《纽约时报》年度十佳书 《GQ》杂志年度十佳书 《卫报》年度十佳书 《泰晤士报》年度十佳书 《经济学人》年度重推! 黑天鹅年度重点图书! 《乌合之众》是为了迎合权贵?《普通心理学》实验存在重大......一起来看看 《千夫所指》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具