asp.net core SqlSugar 多库切换

浪淘沙2年前后端537
  private readonly SqlSugar.ISqlSugarClient _db;

        public AIService(ISqlSugarClient db)
        {
           
            this._db = db.AsTenant().GetConnection("1");
        }
using Furion;
using Masuit.Tools;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using xxtsoft.Common;

namespace xxtsoft.Core
{
    public static class SqlSugarSetup
    {

        public static void AddSqlSugarSetup(this IServiceCollection services, IConfiguration configuration, string conn = "ConnectionStrings:conn")
        {

            /**********多库调用示例*************
              * 
              * .cs
             private readonly SqlSugar.ISqlSugarClient _db;

                public testModel(SqlSugar.ISqlSugarClient db)
                {
                    this._db = db.AsTenant().GetConnection("0");           
                }
                public void OnGet()
                {     
                    dt = _db.Ado.GetDataTable("select * from configuration_properties");
                }
             }    
            * 
            * **************************************/


            SqlSugarScope sqlSugar = new SqlSugarScope(new List<ConnectionConfig>()
            {
               new ConnectionConfig()
               {
                    ConfigId = "0",
                    DbType = DbType.SqlServer,
                    ConnectionString = App.Configuration["ConnectionStrings:conn"],//连接sqlserver符字串
                    IsAutoCloseConnection = true,
                    InitKeyType=InitKeyType.Attribute
                   

               },
               new ConnectionConfig()
               {
                    ConfigId = "1",
                    DbType = DbType.MySql,
                    ConnectionString = App.Configuration["ConnectionStrings:connmszsdev"],//连接mysql符字串
                    IsAutoCloseConnection = true,
                    InitKeyType=InitKeyType.Attribute
                }
              },
                db =>
                {

                    db.Ado.CommandTimeOut = 500;

                    ////单例参数配置,所有上下文生效
                    //db.GetConnection("0").Aop.OnLogExecuting = (sql, pars) =>
                    //{
                    //    if (App.Configuration["debug:sql"].ToLower() == "true")
                    //    {
                    //        FileHelper.FileAdd(FileHelper.absolutPath("log.txt"), "=======" + DateTime.Now + "=======\r\n【ConfigId】1"+  db.CurrentConnectionConfig.ConnectionString + "\r\n【sql】\r\n" + sql + "\r\n【参数】\r\n" + pars.ToJsonString() + "\r\n\r\n");
                    //    }

                    //};
                    //db.GetConnection("1").Aop.OnLogExecuting = (sql, pars) =>
                    //{
                    //    if (App.Configuration["debug:sql"].ToLower() == "true")
                    //    {
                    //        FileHelper.FileAdd(FileHelper.absolutPath("log.txt"), "=======" + DateTime.Now + "=======\r\n【ConfigId】2" + db.CurrentConnectionConfig.ConnectionString + "\r\n【sql】\r\n" + sql + "\r\n【参数】\r\n" + pars.ToJsonString() + "\r\n\r\n");
                    //    }

                    //};

                });

            services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton

        }
    }
}


相关文章

asp.net razor post

asp.net razor post

@page @model xxtsoft.Web.Entry.Pages.qd.IndexModel @{     Layout =&...

点击按钮,禁用提示,加导出word

@page @model xxtsoft.Web.Entry.Pages.sysadmins.kh.kh_outModel @{     Lay...

asp.net core razor 一个页面多个提交

<form method="post">     <button type="submit...

asp.net core 检测上传文件为图片格式

  #region 检测图片格式   var validMimeTypes = new[] { &...

asp.net core postgresql 大数据高性能分页

 /// <summary> /// 分页:返回json格式,参数加密 /// </summary> /// <param nam...

使用 Razor Pages 的多个 Handler

<form method="post" asp-page-handler="Save">    ...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。