public SqlSugarClient GetInstance() { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "Server=.xxxxx", DbType = DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); //每次Sql执行前事件 db.Aop.OnLogExecuting = (sql, pars) => { //我可以在这里面写逻辑 }; return db; } var db=GetInstance(); db.Queryable<T>().ToList();//执行操作会进事件
简化写法
//还有一种写法可以更简单一点 SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.SqlServer, ConnectionString = Config.ConnectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, AopEvents = new AopEvents { OnLogExecuting = (sql, p) => { Console.WriteLine(sql); Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value))); } } });
db.Aop.OnLogExecuted = (sql, pars) => //SQL执行完 { Console.Write("time:" + db.Ado.SqlExecutionTime.ToString());//输出SQL执行时间 }; db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前 { }; db.Aop.OnError = (exp) =>//SQL报错 { //exp.sql 这样可以拿到错误SQL }; db.Aop.OnExecutingChangeSql = (sql, pars) => //可以修改SQL和参数的值 { return new KeyValuePair<string, SugarParameter[]>(sql,pars); };
Sql执行完后会进该事件,该事件可以拿到更改前记录和更改后记录,执行时间等参数
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 }; db.Insertable(new Student() { Name = "beforeName" }) .EnableDiffLogEvent(new { title="我是插入"}) .ExecuteReturnIdentity(); db.Updateable<Student>(new Student() { Id = id, CreateTime = DateTime.Now, Name = "afterName", SchoolId = 2 }) .EnableDiffLogEvent(new { title = "update Student", Modular = 1, Operator = "admin" }) .ExecuteCommand(); db.Deleteable<Student>(id) .EnableDiffLogEvent(new { title = "delete student" }) .ExecuteCommand();
这么改你所有操生成的sql表名前面就加了前缀 select * from "public"."表名"
return new SqlSugarClient(new ConnectionConfig() { DbType = SqlSugar.DbType.PostgreSQL, ConnectionString = Config.ConnectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConfigureExternalServices=new ConfigureExternalServices() { EntityNameService = (type, entity) => { entity.DbTableName = "public." + type.Name; } } });
上面只修改了表,如果我要表和列都修改呢
EntityNameService = (type, entity) => { //修改表 }, EntityService = (type, column) => { //修改列 }
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.SqlServer, ConnectionString = Config.ConnectionString, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true }); db.Aop.OnLogExecuted = (sql, p) => { //执行时间超过1秒 if (db.Ado.SqlExecutionTime.TotalSeconds > 1) { //代码CS文件名 var fileName= db.Ado.SqlStackTrace.FirstFileName; //代码行数 var fileLine = db.Ado.SqlStackTrace.FirstLine; //方法名 var FirstMethodName = db.Ado.SqlStackTrace.FirstMethodName; //db.Ado.SqlStackTrace.MyStackTraceList[1].xxx 获取上层方法的信息 } };
2016 © donet5.comApache Licence 2.0