Skip to content
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

Closed
qiuhaohui163 opened this issue Jul 7, 2020 · 6 comments
Closed

多租户问题 #364

qiuhaohui163 opened this issue Jul 7, 2020 · 6 comments

Comments

@qiuhaohui163
Copy link

如果多租户,能否使用postgresql的多schema模式而非多数据库模式?
在实体中定义Table特性可写入test schema下, 这种写法无法使用变量值.
[Table(Name="test.test")]
使用aop
sql.Aop.ConfigEntity += (s, e) => { e.ModifyResult.Name = "test.test"; //表名 };
还是默认增加到public schema下
或者有其他方式吗?

@2881099
Copy link
Collaborator

2881099 commented Jul 7, 2020

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}"; //表名 };

@qiuhaohui163
Copy link
Author

谢谢巨佬回答.
可以了
之前aop失效
sql.Aop.ConfigEntity += (s, e) => { e.ModifyResult.Name = "test.test"; //表名 };
是因为实体设置了Table特性(Name="test"), 所以e.ModifyResult.Name = "test.test"无法生效
mark一记, 以免后来人采坑吧.

@2881099
Copy link
Collaborator

2881099 commented Jul 7, 2020

文档有说明优先级

@jakey188
Copy link

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}"; //表名 };

这种切换租户后无效

@2881099
Copy link
Collaborator

2881099 commented Jul 11, 2024

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只会执行一次生效。

新版本可以考虑增加这个功能。

@2881099
Copy link
Collaborator

2881099 commented Jul 11, 2024

@jakey188

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants