使用ChangeDatabase 后OnLogExecuting 失效 返回
var client = new SqlSugarClient(ConnectionConfigs);
client.Ado.IsEnableLogEvent = true;
client.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(client.Ado.Connection.ConnectionString); // 用了ChangeDatabase后失效,进不来了
};
--------------
client .AsSugarClient().ChangeDatabase("DbName");
var demoList = client .GetList<tb>(); //执行查询 , 好像还走了主库,没走从库
求解决方案?
热忱回答(11)
-
'猫猫Z VIP0
2019/5/30

强制转换为ITenant后可以实现切库,但OnLogExecuting 事件失败,查询应该也没走从数据库,求解决?
0 回复 -
'猫猫Z VIP0
2019/5/30最新发现无参数时才OnLogExecuting 才失效, 当有参数时能跑进来OnLogExecuting ,求解决?
0 回复 -
'猫猫Z VIP0
2019/6/18强制转换为ITenant后可以实现切库,但OnLogExecuting 事件失败,查询应该也没走从数据库,求解决?
0 回复 -
fate stay night VIP0
2019/6/18不同库是不同的AOP注入事件
0 回复 -
fate stay night VIP0
2019/6/18SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
{
new ConnectionConfig(){ ConfigId="1", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true,AopEvents=事件},
new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true,AopEvents=事件}
});
0 回复 -
fate stay night VIP0
2019/6/18要多库一个事件用这种方式传一个一样的事件对象
0 回复 -
'猫猫Z VIP0
2019/6/191. new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true,AopEvents=事件}
AopEvents 不太明上面的“事件”怎样给值, 可以给个例子给我吗?
2. 为什么当有多个connectionConfigs时下面的方法给值就失效
var client = new SqlSugarClient(ConnectionConfigs);
client.Ado.IsEnableLogEvent = true;
client.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(client.Ado.Connection.ConnectionString);// 无效
};
0 回复 -
'猫猫Z VIP0
2019/6/19在源码里找到了,thanks
private static SqlSugarClient GetInstance()
{
return new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
});
}
0 回复 -
'猫猫Z VIP0
2019/6/19SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
{
new ConnectionConfig(){ ConfigId="1", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true,AopEvents=
new AopEvents
{
OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql);
}
}
},
new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true,AopEvents=
new AopEvents
{
OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql);
}
}
}
});
帮忙看下对不对,测试了一下,还是没进事件里,查看不到查询走的是主库还是从库,请求帮助!
0 回复 -
'猫猫Z VIP0
2019/6/19SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
{
new ConnectionConfig(){ ConfigId="1", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true,AopEvents=事件,IsEnableLogEvent = true},
new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString2 ,InitKeyType=InitKeyType.Attribute ,IsAutoCloseConnection=true,AopEvents=事件,IsEnableLogEvent = true}
});
找到问题了,还要加个IsEnableLogEvent = true
0 回复 -
'猫猫Z VIP0
2019/6/19上面写错ConnectionConfig 没 IsEnableLogEvent属性, 我是重写了一下ChangeDatabase 方法在切完库后重新把AopEvents 与IsEnableLogEvent 重新给值, 建议把IsEnableLogEvent 加到ConnectionConfig 里不然切库后仁德失的。@fate stay night
public ISqlSugarClient ChangeDatabase(string dbname)
{
var BaseAopEvents = Context.CurrentConnectionConfig.AopEvents;
var BaseIsEnableLogEvent = Context.Ado.IsEnableLogEvent;
var myContext = (ITenant)Context;
myContext.ChangeDatabase(dbname);
var newClient = AsSugarClient();
newClient.Ado.IsEnableLogEvent = BaseIsEnableLogEvent;
newClient.CurrentConnectionConfig.AopEvents = BaseAopEvents;
return newClient;
}
0 回复