using Estsh.Core.Config; using SqlSugar; using System; using System.Collections.Generic; namespace Estsh.Core.SqlSugar { /// /// SqlSugar数据库上下文 /// public class SqlSugarDbHelper { /// /// 系统单例上下文对象 /// public static SqlSugarScope SqlSugarDbContext = new SqlSugarScope(new ConnectionConfig() { DbType = (DbType) GlobalConfig.DbConfig.DbType, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConnectionString = GlobalConfig.DbConfig.ConnectionString//主库 }, db => { //单例参数配置,所有上下文生效 db.Ado.CommandTimeOut = GlobalConfig.DbConfig.CommandTimeOut;//数据库超时时间,单位秒 //SQL执行前回调函数 db.Aop.OnLogExecuting = (sql, pars) => { //执行前可以输出SQL //Console.WriteLine(sql); }; //修改SQL和参数的值 db.Aop.OnExecutingChangeSql = (sql, pars) => { if (!String.IsNullOrEmpty(sql) && sql.Contains("CreateTime")) { sql = sql.Replace("CreateTime", "CREATE_TIME"); } return new KeyValuePair(sql, pars); }; //SQL执行完回调函数 db.Aop.OnLogExecuted = (sql, pars) => { //执行完可以输出SQL执行时间 //Console.Write($"SQL:{sql},\r\nTimeSpan:{db.Ado.SqlExecutionTime.TotalMilliseconds}ms\r\n"); }; }); } }