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 @   shiningrise  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
历史上的今天:
2015-09-20 安卓学习资料
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css
点击右上角即可分享
微信分享提示