SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.5 KiB

6 years ago
using SqlSugar;
using System;
8 years ago
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
6 years ago
6 years ago
namespace OrmTest
8 years ago
{
6 years ago
public class DemoB_Aop
8 years ago
{
6 years ago
public static void Init()
{
6 years ago
Console.WriteLine("");
Console.WriteLine("#### Aop Start ####");
6 years ago
6 years ago
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
6 years ago
IsAutoCloseConnection = true
6 years ago
});
6 years ago
db.Aop.OnLogExecuted = (sql, pars) => //SQL executed event
{
Console.WriteLine("OnLogExecuted"+sql);
};
db.Aop.OnLogExecuting = (sql, pars) => //SQL executing event (pre-execution)
{
Console.WriteLine("OnLogExecuting" + sql);
};
db.Aop.OnError = (exp) =>//SQL execution error event
{
//exp.sql
};
db.Aop.OnExecutingChangeSql = (sql, pars) => //SQL executing event (pre-execution,SQL script can be modified)
{
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
};
6 years ago
db.Aop.OnDiffLogEvent = it =>//Get data changes
{
var editBeforeData = it.BeforeData;
var editAfterData = it.AfterData;
var sql = it.Sql;
var parameter = it.Parameters;
var businessData = it.BusinessData;
var time = it.Time;
var diffType = it.DiffType;//enum insert 、update and delete
6 years ago
Console.WriteLine(businessData);
6 years ago
//Write logic
};
6 years ago
6 years ago
3 years ago
var list= db.Queryable<Order>().ToList();
6 years ago
db.Queryable<OrderItem>().ToList();
6 years ago
6 years ago
//OnDiffLogEvent
var data = db.Queryable<Order>().First();
3 years ago
db.Insertable(list.Take(5).ToList()).EnableDiffLogEvent().ExecuteCommand();
db.Insertable(new Order() { CreateTime=DateTime.Now, CustomId=1, Name="a" ,Price=1 }).EnableDiffLogEvent().ExecuteCommand();
6 years ago
data.Name = "changeName";
6 years ago
db.Updateable(data).EnableDiffLogEvent("--update Order--").ExecuteCommand();
6 years ago
3 years ago
db.Updateable(list.Take(5).ToList()).EnableDiffLogEvent("--update Order--").ExecuteCommand();
6 years ago
Console.WriteLine("#### Aop End ####");
6 years ago
}
8 years ago
}
}