using Admin.Core.Common; using Admin.Core.Model; using Microsoft.Extensions.DependencyInjection; using NPOI.SS.Formula.Functions; using SqlSugar; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Admin.Core.Extensions { /// /// SqlSugar 启动服务 /// public static class SqlsugarSetup { public static void AddSqlsugarSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); // 默认添加主数据库连接 MainDb.CurrentDbConnId = Appsettings.app(new string[] { "MainDB" }); // 把多个连接对象注入服务,这里必须采用Scope,因为有事务操作 services.AddScoped(o => { // 连接字符串 var listConfig = new List(); // 从库 var listConfig_Slave = new List(); BaseDBConfig.MutiConnectionString.slaveDbs.ForEach(s => { listConfig_Slave.Add(new SlaveConnectionConfig() { HitRate = s.HitRate, ConnectionString = s.Connection }); }); BaseDBConfig.MutiConnectionString.allDbs.ForEach(m => { listConfig.Add(new ConnectionConfig() { ConfigId = m.ConnId.ObjToString().ToLower(), ConnectionString = m.Connection, DbType = (DbType)m.DbType, IsAutoCloseConnection = true,//自动释放 // Check out more information: https://github.com/anjoy8/Admin.Core/issues/122 MoreSettings = new ConnMoreSettings() { IsWithNoLockQuery = true, //IsAutoToUpper = (DbType)m.DbType== DbType.Oracle? false:true, //禁用自动转成大写表,默认是转大写的可以禁止转大写 IsAutoRemoveDataCache = true }, // 从库 SlaveConnectionConfigs = listConfig_Slave, // 自定义特性 ConfigureExternalServices = new ConfigureExternalServices() { EntityService = (property, column) => { if (column.IsPrimarykey && property.PropertyType == typeof(int)) { column.IsIdentity = true; } } }, InitKeyType = InitKeyType.Attribute } ); }); SqlSugarClient client = new SqlSugarClient(listConfig); // var childA = client.GetConnection("aucma_mes").Ado.SqlQuery("select * from BASE_BOMINFO where OBJ_ID='1'"); return client; }); } private static string GetWholeSql(SugarParameter[] paramArr, string sql) { foreach (var param in paramArr) { sql.Replace(param.ParameterName, param.Value.ObjToString()); } return sql; } private static string GetParas(SugarParameter[] pars) { string key = "【SQL参数】:"; foreach (var param in pars) { key += $"{param.ParameterName}:{param.Value}\n"; } return key; } } }