-
Notifications
You must be signed in to change notification settings - Fork 862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
多租户问题 #364
Comments
public class TenantAccessor : IDisposable
{
static AsyncLocal<string> current = new AsyncLocal<string>();
public static string Current => current.Value ?? "public";
public TenantAccessor(string tenant)
{
current.Value = tenant;
}
public void Dispose()
{
current.Value = null;
}
}
//TenantAccessor 在 mvc 中间件中处理
//aop 事件绑定一次
fsql.Aop.ConfigEntity += (s, e) => { e.ModifyResult.Name = $"{TenantAccessor.Current}.{e.EntityType.Name}"; //表名 }; |
谢谢巨佬回答. |
文档有说明优先级 |
这种切换租户后无效 |
映射只会处理一次,然后缓存起来,所以这里aop只会执行一次生效。 新版本可以考虑增加这个功能。 |
v3.2.833 可以使用 Aop 动态设置 TableName var fsql = new FreeSql.FreeSqlBuilder()
.UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop)
....;
fsql.Aop.ConfigEntity += (s, e) => { e.ModifyResult.Name = $"{TenantAccessor.Current}.{e.EntityType.Name}"; //表名 };
public class TenantAccessor : IDisposable
{
static AsyncLocal<string> current = new AsyncLocal<string>();
public static string Current => current.Value ?? "public";
public TenantAccessor(string tenant)
{
current.Value = tenant;
}
public void Dispose()
{
current.Value = null;
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
如果多租户,能否使用postgresql的多schema模式而非多数据库模式?
在实体中定义Table特性可写入test schema下, 这种写法无法使用变量值.
[Table(Name="test.test")]
使用aop
sql.Aop.ConfigEntity += (s, e) => { e.ModifyResult.Name = "test.test"; //表名 };
还是默认增加到public schema下
或者有其他方式吗?
The text was updated successfully, but these errors were encountered: