EFcore MultiTenat is build on the EF core and asp.net core. It is committed to solve the problem that separated data to different database, schema or table by tenant which have totally different data and resource scope.
to be continue
- dotnet core 3.1
- EntityFrameworkCore 3.1.1
- Pomelo.EntityFrameworkCore.MySql 3.1.1
- Npgsql.EntityFrameworkCore.PostgreSQL 3.1.1
- Microsoft.EntityFrameworkCore.SqlServer 3.1.1
There is not published to any packages management host. so if please clone and build the assembly
- clone
git clone https://github.com/woailibain/kiwiho.EFcore.MultiTenant.git
- build
cd src/kiwiho.EFcore.MultiTenant.Core
dotnet publish
- feel free to choose one of the database implementation. Mysql as the example:
cd src/kiwiho.EFcore.MultiTenant.MySql
dotnet publish
The library have support three traditional modes to seperate the data. But it can combine two of them into the one what you wanted.
- connection
- schema
- table
There are several scenario can be use
-
Only one DbContext is required multiple tenancy. below example will use sql server.
- Add the multi tenant support on function
ConfigureServices
. please one of the line to use.
services.AddSqlServerPerConnection<StoreDbContext>(connectionPrefix: "sqlserver_"); //services.AddSqlServerPerTable<StoreDbContext>(connectionName: "sqlserver_default"); //services.AddSqlServerPerSchema<StoreDbContext>(connectionName: "sqlserver_default");
- Add the default middleware into pipeline on function
Configure
app.UseMiddleware<TenantInfoMiddleware>();
- Add the named
TenantName
on the http request header. The database name or table name or schema name will be end of the TenameName which was provided on the header. For sure, if the TenantName isn't existing that may occur the some kind of connection error message.
- Add the multi tenant support on function
-
Multiple DbContext are required. Multiple DbContext integration were considered in the beginning of the library coding. Hence, use the property
Key
on theTenantSetting
to match the differentConnectionGernerator
by the propertyKey
equality check or the result of functionMatchTenantKey
. Meanwhile, different database engine or different mode can be mixed.- Add multiple lines on function
ConfigureServices
.
services.AddSqlServerPerSchema<StoreDbContext>("store", "sqlserver_default"); services.AddMySqlPerTable<CustomerDbContext>("customer", "sqlserver_default");
- Add the default middleware in to pipeline on function
Configure
app.UseMiddleware<TenantInfoMiddleware>();
By the way, there is a full example persistence on the source code on
example/traditional_and_multiple_context
directory. - Add multiple lines on function
-
Combine two modes intended for the same DbContext to reduce the number of database, schema or table. For example, you may have to set up a large quanity of database instances due to the tenant number grow up. But a part of the oridnary tenants are not necessary to have an indenpence database instance, a good way is to group the small tenants into one database and seperate them by table or schema. To override the
ConnectionGenerator
and add the rules caculate which database/schema/tabase it is. For the fully implementation, please check the example source code on directoryexample/mix_mode
-
Exchange to Read/Write splitting. There is more complex than preceding scenario, please reference the example
example/read_write_splitting
directly.
This library was come here when I am writing the blog. As the title you seen, there are writed by Chinese, hope you can get the guidence on the example along with the source code here. Please feel free to reference below blogs.