init commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using Furion;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.EntityFramework.Core.SqlSugar
|
||||
{
|
||||
public static class SqlsugarSetup
|
||||
{
|
||||
public static void AddSqlsugarSetup(this IServiceCollection services)
|
||||
{
|
||||
var connList = App.GetConfig<List<MutiDBConnectionString>>("ConnectionStrings")?.Where(p => p.Enabled).ToList();
|
||||
|
||||
List<ConnectionConfig> connectConfigList = new();
|
||||
connList.ForEach(conn =>
|
||||
{
|
||||
connectConfigList.Add(new ConnectionConfig
|
||||
{
|
||||
ConfigId = conn.Id,
|
||||
ConnectionString = conn.ConnectionString,
|
||||
DbType = conn.DbType,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute
|
||||
});
|
||||
});
|
||||
services.AddSqlSugar(connectConfigList.ToArray(), db =>
|
||||
{
|
||||
//处理日志事务
|
||||
db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
{
|
||||
Debugger.Log(1, "", $"===================CurrentConfigId:{db.CurrentConnectionConfig.ConfigId} SqlSugar log started ===================\r\n");
|
||||
Debugger.Log(2, "语句:", sql);
|
||||
Debugger.Log(3, "参数:", string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
Debugger.Log(4, "", $"===================CurrentConfigId:{db.CurrentConnectionConfig.ConfigId} SqlSugar log end ===================\r\n");
|
||||
//App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
|
||||
////如果多个数数据库传 List<ConnectionConfig>
|
||||
//var configConnection = new ConnectionConfig()
|
||||
//{
|
||||
// DbType = SqlSugar.DbType.MySql,
|
||||
// ConnectionString = configuration.GetConnectionString(dbName),
|
||||
// IsAutoCloseConnection = true,
|
||||
//};
|
||||
|
||||
//SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
|
||||
// db =>
|
||||
// {
|
||||
// //单例参数配置,所有上下文生效
|
||||
// db.Aop.OnLogExecuting = (sql, pars) =>
|
||||
// {
|
||||
// //Console.WriteLine(sql);//输出sql
|
||||
// };
|
||||
// });
|
||||
|
||||
//services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
|
||||
}
|
||||
}
|
||||
|
||||
public class MutiDBConnectionString
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string ConnectionString { get; set; }
|
||||
public DbType DbType { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
public string ProviderName { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user