-
Notifications
You must be signed in to change notification settings - Fork 1.3k
b2.Aop‐Data events
果糖网 edited this page Jul 21, 2024
·
8 revisions
Only entity operations are supported
Events Description
// Add, delete, change before and after DataExecuting is the same as DataChangesExecuted
DataExecuting // before
DataChangesExecuted//after (the same usage as DataExecuting)
// Query data processing events
DataExecuted
public SqlSugarClient GetInstance()
{
SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "Server=.xxxxx",
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
},
db=>{
db.Aop.DataExecuting = (oldValue, entityInfo) =>
{
}
});
return db;
}
var db=GetInstance();
db.Queryable<T>().ToList();//执行操作会进事件
This event can be added (Title 2.1), changed (Title 2.2), and deleted (Title 2.3).
db.Aop.DataExecuting = (oldValue, entityInfo) =>
{
/*** column-level events: Every column you insert carries an event ***/
if (entityInfo.PropertyName == "CreateTime"&&entityInfo.OperationType== DataFilterType.InsertByObject)
{
entityInfo.SetValue(DateTime.Now); // Modify the CreateTime field
/*entityInfo has field all parameters */
/*oldValue indicates that the current field value is the same as */
//var value=entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
/* Gets the current column feature */
//5.1.3.23 +
/ / entityInfo IsAnyAttribute characteristics of < > ()
/ / entityInfo GetAttribute characteristics of < > ()
}
/*** * row-level events: A record is entered only once ***/
if(entityInfo.EntityColumnInfo.IsPrimarykey)
{
//entityInfo.EntityValue gets a single entity object
}
// Multiple IFs can be written
};
/*** can only be an entity insert ***/
db.Insertable(T).ExecuteReturnIdentity();
db.Insertable(List<T>).ExecuteReturnIdentity();
db.Aop.DataExecuting = (oldValue, entityInfo) =>
{
/*** column-level events: Each column that is updated carries an event ***/
if (entityInfo.PropertyName =="UpdateTime" && entityInfo.OperationType == DataFilterType.UpdateByObject)
{
entityInfo.SetValue(DateTime.Now); // Modify the UpdateTime field
/* Current column gets feature */
//5.1.3.23 +
/ / entityInfo IsAnyAttribute characteristics of < > ()
/ / entityInfo GetAttribute characteristics of < > ()
}
/*** Row level events: Update a record only once ***/
if(entityInfo.EntityColumnInfo.IsPrimarykey)
{
//entityInfo.EntityValue gets a single entity object
}
/*** Modify another column ***/ based on the current column
//if(current column logic ==XXX)
//var properyDate = entityInfo.EntityValue.GetType().GetProperty("Date");
//if(properyDate! =null)
//properyDate.SetValue(entityInfo.EntityValue,1);
// Multiple IFs can be written
};
/*** Physical mode update ***/
db.Updateable(data).ExecuteCommand();
db.Updateable(dataList).ExecuteCommand();
// Specify column append filter (5.1.4.106-Preview19+)
db.Updateable(updateObj).UpdateColumns(it=>new {it.Name},true).ExecuteCommand()
/*** Expression update ***/
// New 5.1.3.2: Expression update supports data filters
db.Updateable<Order>()
.SetColumns(it => new Order(){ Name="A" }
AppendColumnsByDataFilter: true) / / true table type added filter assignment fields
.Where(it=>it.Id==1).ToSqlString()
This event can be added (Title 2.1), changed (Title 2.2), and deleted (Title 2.3).
db.Aop.DataExecuting = (oldValue, entityInfo) =>
{
/*** Deletion effective (row level events only) ***/
if (entityInfo.OperationType == DataFilterType.DeleteByObject)
{
//entityInfo.EntityValue gets all entity objects
}
// Multiple IFs can be written
}
/*** can only be entities deleted ***/
db.Deleteable(List<T>).ExecCommand(); // The entity collection is valid
db.Deleteable(T).ExecCommand(); // Entity valid
New function :5.1.2-preview05
// It can only be an entity query and cannot be an anonymous object
db.Aop.DataExecuted = (value, entity) =>
{
// Only row-level events
if (entity.Entity.Type == typeof(Order))
{
var newValue=entity.GetValue(nameof(Order.Name))+"111";
entity.SetValue(nameof(Order.Name), newValue);
}
};
// The name of the queried value is added with 111
List<Order> list2 = db.Queryable<Order>().ToList();