.net core web api 中BaseRepository配置(ORM使用的是sqlsugar)
public class BaseRepository<T> : IBaseRepository<T> where T : class, new() { protected SqlSugarClient db => GetInstance(); protected virtual SqlSugarClient GetInstance() { SqlSugarClient db = new SqlSugarClient( new ConnectionConfig() { ConnectionString = DbContext.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true, IsShardSameThread = true,/*Shard Same Thread*/ });
//记录SQL语句到日志 db.Aop.OnLogExecuted = (sql, pars) => //SQL执行完 { Logger.Debug($"{sql} \r\n param {string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value))} \r\n 执行时间{db.Ado.SqlExecutionTime.TotalMilliseconds}ms"); }; return db; } }