/// <summary></summary> /// <param name="services"></param> public static void AddSqlsugarSetup(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } // 缓存 ICacheService myCache = new SugarCache(); #region 数据库连接服务注入 var configuration = BaseConfigModel.Configuration; // 连接字符串 var listConfig = new List <IocConfig>(); var databaseConfig = configuration.GetSection("database").Get <List <Dbconfig> >(); if (databaseConfig.Count == 0) { throw new ArgumentNullException("请配置数据库连接");// } databaseConfig.Where(c => c.isclose == false).ToList().ForEach(m => { listConfig.Add(new IocConfig() { ConfigId = m.dbname, ConnectionString = m.dburl, DbType = (IocDbType)m.dbtype, IsAutoCloseConnection = true, } ); }); services.AddSqlSugar(listConfig); // services.AddSqlsugarSetup(); #endregion 数据库连接服务注入 }
/// <summary></summary> /// <param name="services"></param> public static void AddSqlsugarSetup(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } // 缓存 ICacheService myCache = new SugarCache(); // 把多个连接对象注入服务,这里必须采用Scope,因为有事务操作 services.AddScoped <ISqlSugarClient>(o => { var configuration = BaseConfigModel.Configuration; // 连接字符串 var listConfig = new List <ConnectionConfig>(); var databaseConfig = configuration.GetSection("database").Get <List <Dbconfig> >(); if (databaseConfig.Count == 0) { throw new ArgumentNullException("请配置数据库连接");// } databaseConfig.Where(c => c.isclose == false).ToList().ForEach(m => { listConfig.Add(new ConnectionConfig() { ConfigId = m.dbname, ConnectionString = m.dburl, DbType = (DbType)m.dbtype, IsAutoCloseConnection = true, IsShardSameThread = false, ConfigureExternalServices = new ConfigureExternalServices() { DataInfoCacheService = myCache //配置我们创建的缓存类 }, //db.Aop.OnDiffLogEvent = it => //{ // var editBeforeData = it.BeforeData;//操作前记录 包含: 字段描述 列名 值 表名 表描述 // var editAfterData = it.AfterData;//操作后记录 包含: 字段描述 列名 值 表名 表描述 // var sql = it.Sql; // var parameter = it.Parameters; // var data = it.BusinessData;//这边会显示你传进来的对象 // var time = it.Time; // var diffType = it.DiffType;//enum insert 、update and delete // //Write logic //}; AopEvents = new AopEvents { OnDiffLogEvent = it => { }, OnLogExecuting = (sql, p) => { string Parameter = GetParas(p); // CreateLog.Info(Parameter) } }, MoreSettings = new ConnMoreSettings() { IsAutoRemoveDataCache = true } } ); }); return(new SqlSugarClient(listConfig)); }); }