SqlSugar/Src/Asp.Net/SqlServerTest/Demos/9_Aop.cs

89 lines
2.4 KiB
C#
Raw Normal View History

2018-11-13 00:35:42 +08:00
using OrmTest.Models;
using SqlSugar;
2017-09-19 00:29:30 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class Aop
{
public static void Init()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
2018-11-13 01:51:36 +08:00
2018-11-25 13:19:59 +08:00
2017-09-19 00:29:30 +08:00
db.Aop.OnLogExecuted = (sql, pars) =>
{
2018-11-13 00:07:07 +08:00
Console.Write("time:" + db.Ado.SqlExecutionTime.ToString());
2017-09-19 00:29:30 +08:00
};
db.Aop.OnLogExecuting = (sql, pars) =>
{
};
db.Aop.OnError = (exp) =>
{
2018-11-13 01:51:36 +08:00
2017-09-19 00:29:30 +08:00
};
db.Aop.OnExecutingChangeSql = (sql, pars) =>
{
2018-11-13 01:51:36 +08:00
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
2017-09-19 00:29:30 +08:00
};
db.Queryable<CMStudent>().ToList();
try
{
db.Queryable<CMStudent>().AS(" ' ").ToList();
}
catch (Exception)
{
2018-11-13 01:51:36 +08:00
2017-09-19 00:29:30 +08:00
}
2018-11-13 00:35:42 +08:00
//diff log demo
2018-11-13 01:51:36 +08:00
db.Aop.OnDiffLogEvent = it =>
2018-11-13 00:35:42 +08:00
{
var editBeforeData = it.BeforeData;
2018-11-26 18:23:53 +08:00
var editAfterData = it.AfterData;
2018-11-13 00:35:42 +08:00
var sql = it.Sql;
var parameter = it.Parameters;
2018-11-13 01:51:36 +08:00
var data = it.BusinessData;
2018-11-27 16:26:05 +08:00
var type = it.DiffType;
var time = it.Time;
2018-11-13 00:35:42 +08:00
};
2018-11-13 01:51:36 +08:00
2018-11-25 13:19:59 +08:00
var id = db.Insertable(new Student() { Name = "beforeName" })
.EnableDiffLogEvent(new { title="add student"})
.ExecuteReturnIdentity();
2018-11-13 01:51:36 +08:00
db.Updateable<Student>(new Student()
{
Id = id,
CreateTime = DateTime.Now,
2018-11-22 00:36:18 +08:00
Name = "afterName",
2018-11-13 01:51:36 +08:00
SchoolId = 2
})
2018-11-25 13:19:59 +08:00
.EnableDiffLogEvent(new { title = "update Student", Modular = 1, Operator = "admin" })
.ExecuteCommand();
db.Deleteable<Student>(id)
.EnableDiffLogEvent(new { title = "delete student" })
2018-11-13 01:51:36 +08:00
.ExecuteCommand();
2018-11-22 00:36:18 +08:00
2017-09-19 00:29:30 +08:00
2018-11-25 15:45:24 +08:00
//primary key guid
db.Insertable(new DataTestInfo2() { Bool1=true, Bool2=false, PK=Guid.NewGuid(), Text1="a" })
.EnableDiffLogEvent(new { title = "add DataTestInfo2" })
.ExecuteReturnIdentity();
}
2018-11-13 01:51:36 +08:00
}
2017-09-19 00:29:30 +08:00
}