Admin.Net根据域名自动选择数据库

namespace Admin.NET.Core;

/// <summary>
/// SqlSugar 实体仓储
/// </summary>
/// <typeparam name="T"></typeparam>
public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
{
    protected ITenant iTenant = null;

    public SqlSugarRepository()
    {
        var host = App.HttpContext?.Request.Host.Value.ToString();
        if(string.IsNullOrEmpty(host))
        {
            host = "";
        }
        if(host.Contains("localhost") || host.Contains("127.0.0.1"))
        {
            base.Context = App.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope(SqlSugarConst.MainConfigId);
            return;
        }
        else if (host.Contains("npsy.wxy.work"))
        {
            base.Context = App.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("npsy");
            return;
        }
        else if (host.Contains("npjj.wxy.work"))
        {
            base.Context = App.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("npjj");
            return;
        }
        else if (host.Contains("19.wxy.work"))
        {
            base.Context = App.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("npjx");
            return;
        }
        else
        {
            base.Context = App.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope(SqlSugarConst.MainConfigId);
            return;
        }
    }
}

posted @ 2024-09-20 18:39  shiningrise  阅读(45)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css