From deea9c14574df67baf5be8fd2477a079cfa96f30 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Wed, 19 Apr 2023 16:34:47 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?=
=?UTF-8?q?=E6=A0=B9=E6=8D=AETracking=E8=BF=9B=E8=A1=8C=E6=9B=B4=E6=96=B0?=
=?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../DatabaseDefinitions/SugarDbContext.cs | 100 +++++++++++-------
1 file changed, 63 insertions(+), 37 deletions(-)
diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs
index 844b77f..028ef56 100644
--- a/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs
+++ b/Falcon.SugarApi/DatabaseDefinitions/SugarDbContext.cs
@@ -10,20 +10,20 @@ namespace Falcon.SugarApi.DatabaseDefinitions
///
/// Sugar数据库上下文
///
- public class SugarDbContext : SqlSugarClient
+ public class SugarDbContext:SqlSugarClient
{
///
/// 通过链接配置ConnectionConfig构造SqlSugarClient数据上下文
///
///
/// 日志记录器
- public SugarDbContext(SugarConnectionConfig config, ILogger logger) : base(config) {
+ public SugarDbContext(SugarConnectionConfig config,ILogger logger) : base(config) {
this.Logger = logger;
- if (config.Log) {
- this.Aop.OnLogExecuting = (string sql, SugarParameter[] paras) => {
+ if(config.Log) {
+ this.Aop.OnLogExecuting = (string sql,SugarParameter[] paras) => {
StringBuilder sb = new();
sb.AppendLine(sql);
- sb.AppendLine(this.Utilities.SerializeObject(paras.ToDictionary(it => it.ParameterName, it => it.Value)));
+ sb.AppendLine(this.Utilities.SerializeObject(paras.ToDictionary(it => it.ParameterName,it => it.Value)));
this.Logger.LogInformation(sb.ToString());
};
}
@@ -40,17 +40,17 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 数据实体类型
/// 要插入的数据列表
/// 创建人
- public void Insert(List data, string createBy) where T : class, new() {
- foreach (var i in data) {
- if (i is ICreateNew cn) {
+ public void Insert(List data,string createBy) where T : class, new() {
+ foreach(var i in data) {
+ if(i is ICreateNew cn) {
cn.CreateNew(createBy);
}
}
try {
this.Insertable(data).ExecuteCommand();
}
- catch (Exception ex) {
- throw new Exception("SugarDbContext.Insert数据发生异常!", ex);
+ catch(Exception ex) {
+ throw new Exception("SugarDbContext.Insert数据发生异常!",ex);
}
}
@@ -60,8 +60,8 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 数据模型
/// 要插入的数据
/// 创建人
- public void Insert(T data, string createBy) where T : class, new() {
- this.Insert(new List { data }, createBy);
+ public void Insert(T data,string createBy) where T : class, new() {
+ this.Insert(new List { data },createBy);
}
#endregion
@@ -73,9 +73,9 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 数据模型
/// 要更新的数据
/// 更新人
- public List Update(List data, string updateBy) where T : class, new() {
- foreach (var i in data) {
- if (i is IModify cn) {
+ public List Update(List data,string updateBy) where T : class, new() {
+ foreach(var i in data) {
+ if(i is IModify cn) {
cn.Modify(updateBy);
}
}
@@ -89,11 +89,37 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 数据模型
/// 要更新的数据
/// 更新人
- public T Update(T data, string updateBy) where T : class, new() {
- this.Update(new List { data }, updateBy);
+ public T Update(T data,string updateBy) where T : class, new() {
+ this.Update(new List { data },updateBy);
return data;
}
+ ///
+ /// 利用Tracking跟踪并更新数据
+ ///
+ /// 要跟踪更新的实体类型
+ /// 要跟踪更新的实体数据
+ /// 修改实体数据
+ public void Update(T data,Action changeValfun) where T : class, new() {
+ if(data == null) return;
+ this.Tracking(data);
+ changeValfun?.Invoke(data);
+ this.Updateable(data).ExecuteCommand();
+ }
+
+ ///
+ /// 利用Tracking跟踪并更新数据
+ ///
+ /// 要跟踪更新的实体类型
+ /// 要跟踪更新的实体数据
+ /// 修改实体数据
+ public async void UpdateAsync(T data,Action changeValfun) where T : class, new() {
+ if(data == null) return;
+ this.Tracking(data);
+ changeValfun?.Invoke(data);
+ await this.Updateable(data).ExecuteCommandAsync();
+ }
+
#endregion
#region 软删除
@@ -103,7 +129,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 数据实体类型,必须继承自SugarTableBase
/// 要删除的数据id
/// 删除人
- public void Delete(Guid id, string deleteBy) where T : SugarTableBase, new() {
+ public void Delete(Guid id,string deleteBy) where T : SugarTableBase, new() {
var data = new T();
data.Delete(deleteBy);
this.Updateable()
@@ -121,9 +147,9 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 实体对象列表
/// 删除人
/// 删除后的实体列表
- public List Delete(List data, string deleteBy) where T : class, new() {
- foreach (var item in data) {
- if (item is IDelete d) {
+ public List Delete(List data,string deleteBy) where T : class, new() {
+ foreach(var item in data) {
+ if(item is IDelete d) {
d.Delete(deleteBy);
}
}
@@ -137,8 +163,8 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 实体对象
/// 删除人
/// 删除后的对象
- public T Delete(T data, string deleteBy) where T : class, new() {
- this.Delete(new List { data }, deleteBy);
+ public T Delete(T data,string deleteBy) where T : class, new() {
+ this.Delete(new List { data },deleteBy);
return data;
}
@@ -155,11 +181,11 @@ namespace Falcon.SugarApi.DatabaseDefinitions
///
/// 要升级的表模型
public void UpdateTableStructure(params Type[] types) {
- for (int i = 0; i < types.Length; i++) {
+ for(int i = 0;i < types.Length;i++) {
var type = types[i];
- if (!InitdTable.Any(m => m == type.FullName)) {
+ if(!InitdTable.Any(m => m == type.FullName)) {
this.CodeFirst.BackupTable().SetStringDefaultLength(200).InitTables(type);
- if (type.FullName != null) {
+ if(type.FullName != null) {
InitdTable.Add(type.FullName);
}
}
@@ -181,10 +207,10 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 初始化表的模型
/// 表名
/// 表首次创建后执行的回调
- public void UpdateTable(string tableName, Action callbackWhenInitTable) {
- var hasTable = this.DbMaintenance.IsAnyTable(tableName, false);
+ public void UpdateTable(string tableName,Action callbackWhenInitTable) {
+ var hasTable = this.DbMaintenance.IsAnyTable(tableName,false);
this.CodeFirst.InitTables();
- if (!hasTable) {
+ if(!hasTable) {
callbackWhenInitTable(this);
}
}
@@ -206,7 +232,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 参数名称
///
public SugarParameter OracleRefCursor(string name = ":v_data") =>
- new(name, null, true) { IsRefCursor = true, DbType = System.Data.DbType.String };
+ new(name,null,true) { IsRefCursor = true,DbType = System.Data.DbType.String };
///
/// 从对象获取存储过程参数
@@ -215,18 +241,18 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 参数对象
/// 增加其他的参数
/// 参数
- public SugarParameter[] GetParameters(T data, params SugarParameter[] otherParams) {
+ public SugarParameter[] GetParameters(T data,params SugarParameter[] otherParams) {
var result = new List();
- if (data != null) {
- var dict = new Dictionary();
- foreach (var p in data.GetType().GetProperties()) {
- if (p.CanRead) {
- dict.Add(p.Name, p.GetValue(data) ?? "");
+ if(data != null) {
+ var dict = new Dictionary();
+ foreach(var p in data.GetType().GetProperties()) {
+ if(p.CanRead) {
+ dict.Add(p.Name,p.GetValue(data) ?? "");
}
}
result.AddRange(this.Ado.GetParameters(dict).ToArray());
}
- if (otherParams != null) {
+ if(otherParams != null) {
result.AddRange(otherParams);
}
return result.ToArray();