From 4a5f07d56b96feb34c1d18c73755e23dc7002f07 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@users.noreply.github.com> Date: Thu, 11 Jul 2024 19:36:43 +0800 Subject: [PATCH] Update multi-tenancy.md --- docs/guide/multi-tenancy.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/guide/multi-tenancy.md b/docs/guide/multi-tenancy.md index cc437c8782..00320f541e 100644 --- a/docs/guide/multi-tenancy.md +++ b/docs/guide/multi-tenancy.md @@ -135,6 +135,41 @@ var goodsRepository = fsql.GetRepository(null, old => $"{Goods}_{TenantMa 上面我们得到一个仓储按租户分表,使用它 CURD 最终会操作 Goods_1 表。 +> v3.2.833 动态设置表名 + +```csharp +var fsql = new FreeSql.FreeSqlBuilder() + .UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop) + ....; +fsql.Aop.ConfigEntity += (s, e) => { e.ModifyResult.Name = $"{TenantAccessor.Current}.{e.ModifyResult.Name}"; //表名 }; + +app.Use(async (context, next) => +{ + // 使用者通过 aspnetcore 中间件,解析 token 得到租户信息 + string tenant = YourGetTenantFunction(); + using (new TenantAccessor(tenant)) + { + await next(); + } +}); + +public class TenantAccessor : IDisposable +{ + static AsyncLocal current = new AsyncLocal(); + public static string Current => current.Value ?? "public"; + + public TenantAccessor(string tenant) + { + current.Value = tenant; + } + + public void Dispose() + { + current.Value = null; + } +} +``` + > 更多说明参考:[《FreeSql.Repository 仓储》](repository.md)、[《分表分库》](sharding.md)。 ### 方案三:按租户分库