6K Star 11.8K Fork 4K

GVPdotNET China / Furion

 / 详情

Furion.Extras.DatabaseAccessor.SqlSugar 配置多个数据库打印 SQL 语句问题

已完成
创建于  
2021-03-03 08:30

Furion 版本号

1.15.0


Web 项目类型

  • WebApi
  • Mvc
  • Razor Pages
  • Blazor Server

描述你的问题

数据库连接配置:

{
  "ConnectionConfigs": [
    {
      "ConfigId": "testgoods",
      "ConnectionString": "",
      "DbType": "MySql",
      "IsAutoCloseConnection": true,
      "InitKeyType": "Attribute"
    },
    {
      "ConfigId": "taihedb",
      "ConnectionString": "",
      "DbType": "MySql",
      "IsAutoCloseConnection": true,
      "InitKeyType": "Attribute"
    }
  ]
}

代码实现:

List<ConnectionConfig> connectionConfigs = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
services.AddSqlSugar(connectionConfigs.ToArray(), db => db.Aop.OnLogExecuting = (sql, pars) => {
    string parsInfo = string.Empty;
    if (0 != pars.Length) {
        parsInfo = string.Join(",", pars?.Select(it => it.ParameterName + ": " + it.Value + (null == it.Value ? string.Empty : string.Format("({0})", it.Value.GetType().Name))));
    }
    Debug.WriteLine(sql);
    Debug.WriteLineIf(0 != pars.Length, parsInfo);

    App.PrintToMiniProfiler("SqlSugar", "Info", sql + (0 == pars.Length ? string.Empty : ("\r\n" + parsInfo)));
});

两个库分别执行SQL语句。

// "ConfigId": "testgoods"
var list1 = db.Queryable<dynamic>().AS("js_gen_table").ToList();
// "ConfigId": "taihedb",
var list2 = (db as SqlSugarClient).GetConnection("taihedb").Queryable<dynamic>().AS("js_sys_company").ToList();

问题是执行第二个查询语句不能打印SQL语句。
在别的项目测试 SqlSugar, 多个数据库是可以打印SQL语句。

请教是我代码实现有问题吗?如果我代码实现有问题,应该如何实现。


数据库信息

  • Sqlite
  • SqlServer
  • Mysql
  • Oracle
  • PGSql
  • Firebird
  • Cosmos

期待结果

多个数据库都能打印SQL语句。


评论 (11)

伪装者 创建了任务
伪装者 关联仓库设置为dotNET China/Furion
伪装者 修改了描述
伪装者 修改了描述
伪装者 修改了描述
伪装者 修改了描述
百小僧 修改了描述
展开全部操作日志

我正在测试。

百小僧 任务状态待办的 修改为进行中
百小僧 负责人设置为百小僧
百小僧 添加协作者dotNET China
百小僧 添加了
 
疑问
标签
百小僧 添加了
 
无法实现
标签
百小僧 里程碑设置为Furion 2021
百小僧 关联分支设置为master
百小僧 计划截止日期设置为2021-03-03
百小僧 置顶等级设置为
百小僧 优先级设置为严重

貌似新版本用的是 db.ChangeDatabase("2") 切换。

能否写一个其他项目正常的例子我?弄一个git仓库,我去看看。不用furion的版本。

百小僧 任务状态进行中 修改为待办的
百小僧 通过 dotnetchina/Furion Commit d20263d任务状态待办的 修改为已完成
百小僧 置顶等级 修改为不置顶

那个要循环添加aop

能否写一个其他项目正常的例子我?弄一个git仓库,我去看看。不用furion的版本。

@百小僧
SqlSugarClient client = new SqlSugarClient(new ConnectionConfig() {
ConfigId = "testgoods",
ConnectionString = "",//连接符字串
DbType = SqlSugar.DbType.MySql,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute, //从特性读取主键自增信息
AopEvents = new AopEvents {
OnLogExecuting = (sql, p) => {
Debug.WriteLine("testgoods => " + sql);
Debug.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
}); ;

client.AddConnection(new ConnectionConfig() {
ConfigId = "taihedb",
ConnectionString = "",//连接符字串
DbType = SqlSugar.DbType.MySql,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute, //从特性读取主键自增信息
AopEvents = new AopEvents {
OnLogExecuting = (sql, p) => {
Debug.WriteLine("taihedb => " + sql);
Debug.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
});

var list1 = client.Queryable().AS("js_gen_table").ToList();
var list2 = client.GetConnection("taihedb").Queryable().AS("js_sys_company").ToList();

那个要循环添加aop

@百小僧 |

要循环添加aop是可以打印 SQL。 但是,不能像上面的代码打印出是那个数据库执行的 SQL 语句。
aop 事件每次调用的都是最后一次添加的aop事件代码。

List connectionConfigs = App.GetConfig<List>("ConnectionConfigs");
connectionConfigs[0].AopEvents = new AopEvents {
OnLogExecuting = (sql, pars) => {
#if DEBUG
string configId = "“SqlSugar testgoods” ";
string parsInfo = string.Empty;
if (0 != pars.Length) {
parsInfo = string.Join(",", pars?.Select(it => it.ParameterName + ": " + it.Value + (null == it.Value ? string.Empty : string.Format("({0})", it.Value.GetType().Name))));
}
Debug.WriteLine(configId + sql);
if (0 != pars.Length) Debug.WriteLine(configId + parsInfo);

    App.PrintToMiniProfiler("SqlSugar", "Info", sql + (0 == pars.Length ? string.Empty : ("\r\n" + parsInfo)));

#endif
}
};
services.AddSqlSugar(connectionConfigs[0]);

connectionConfigs[1].AopEvents = new AopEvents {
OnLogExecuting = (sql, pars) => {
#if DEBUG
string configId = "“SqlSugar taihedb” ";
string parsInfo = string.Empty;
if (0 != pars.Length) {
parsInfo = string.Join(",", pars?.Select(it => it.ParameterName + ": " + it.Value + (null == it.Value ? string.Empty : string.Format("({0})", it.Value.GetType().Name))));
}
Debug.WriteLine(configId + sql);
if (0 != pars.Length) Debug.WriteLine(configId + parsInfo);

    App.PrintToMiniProfiler("SqlSugar", "Info", sql + (0 == pars.Length ? string.Empty : ("\r\n" + parsInfo)));

#endif
}
};
services.AddSqlSugar(connectionConfigs[1]);

伪装者 任务状态已完成 修改为待办的

可能这个和SqlSugar了解一下,如果是Furion的问题,一定修复。

百小僧 任务状态待办的 修改为已完成

这个Issue暂时关闭哈。等确认了是哪方的问题后再开启。

PG数据库怎么配置

ZhengSheng 修改了描述
ZhengSheng 修改了描述
ZhengSheng 修改了描述
ZhengSheng 修改了描述
ZhengSheng 修改了描述
ZhengSheng 修改了描述
ZhengSheng 关联分支master 修改为未关联

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(4)
974299 monksoul 1578937227 1136896 xueshan2018 1610861526 8055741 chinadotnet 1606890988
C#
1
https://gitee.com/dotnetchina/Furion.git
git@gitee.com:dotnetchina/Furion.git
dotnetchina
Furion
Furion

搜索帮助