From cf8dfc0d190ca0f8222a509dda6d27337ccefce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Fri, 21 Jul 2023 18:17:58 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=E4=BC=98=E5=8C=96codefirst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.Admin.WebApi/Extensions/DbExtension.cs | 12 ++++---- ZR.Admin.WebApi/Extensions/InitTable.cs | 4 +-- ZR.Model/System/Article.cs | 5 +--- ZR.Model/System/SqlDiffLog.cs | 34 +++++++++++++++++++++-- ZR.Model/System/SysDept.cs | 12 ++++---- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/ZR.Admin.WebApi/Extensions/DbExtension.cs b/ZR.Admin.WebApi/Extensions/DbExtension.cs index 8dd60e1..555fe0b 100644 --- a/ZR.Admin.WebApi/Extensions/DbExtension.cs +++ b/ZR.Admin.WebApi/Extensions/DbExtension.cs @@ -2,7 +2,6 @@ using Infrastructure; using Infrastructure.Extensions; using SqlSugar; using SqlSugar.IOC; -using System.Security.Principal; using ZR.Model; using ZR.Model.System; @@ -125,20 +124,19 @@ namespace ZR.Admin.WebApi.Extensions SqlDiffLog log = new() { - AfterData = pars, - BusinessData = data, + BeforeData = pars, + BusinessData = data.ToString(), DiffType = diffType.ToString(), Sql = sql, TableName = item.TableName, UserName = name, - AddTime = DateTime.Now + AddTime = DateTime.Now, + ConfigId = configId }; //logger.WithProperty("title", data).Info(pars); - db.GetConnectionScope(configId).Insertable(log).ExecuteReturnSnowflakeId(); + db.GetConnectionScope(0).Insertable(log).ExecuteReturnSnowflakeId(); } } - - //Write logic }; db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() { diff --git a/ZR.Admin.WebApi/Extensions/InitTable.cs b/ZR.Admin.WebApi/Extensions/InitTable.cs index 69bb24a..2997b82 100644 --- a/ZR.Admin.WebApi/Extensions/InitTable.cs +++ b/ZR.Admin.WebApi/Extensions/InitTable.cs @@ -23,8 +23,7 @@ namespace ZR.Admin.WebApi.Extensions //var entityes = AssemblyUtils.GetAllTypes().Where(p => !p.IsAbstract && p != baseType && p.GetCustomAttribute() != null).ToArray(); //db.CodeFirst.InitTables(entityes); - //23个表,建议先使用下面方法初始化表,方便排查问题 - db.CodeFirst.InitTables(typeof(SqlDiffLog)); + //24个表,建议先使用下面方法初始化表,方便排查问题 db.CodeFirst.InitTables(typeof(SysUser)); db.CodeFirst.InitTables(typeof(SysRole)); db.CodeFirst.InitTables(typeof(SysDept)); @@ -48,6 +47,7 @@ namespace ZR.Admin.WebApi.Extensions db.CodeFirst.InitTables(typeof(ArticleCategory)); db.CodeFirst.InitTables(typeof(SysDictData)); db.CodeFirst.InitTables(typeof(SysDictType)); + db.CodeFirst.InitTables(typeof(SqlDiffLog)); } } diff --git a/ZR.Model/System/Article.cs b/ZR.Model/System/Article.cs index 52854ae..7cb60f2 100644 --- a/ZR.Model/System/Article.cs +++ b/ZR.Model/System/Article.cs @@ -1,7 +1,4 @@ -using SqlSugar; -using System; - -namespace ZR.Model.System +namespace ZR.Model.System { /// /// 文章表 diff --git a/ZR.Model/System/SqlDiffLog.cs b/ZR.Model/System/SqlDiffLog.cs index c09f9cc..0b64277 100644 --- a/ZR.Model/System/SqlDiffLog.cs +++ b/ZR.Model/System/SqlDiffLog.cs @@ -1,21 +1,51 @@ namespace ZR.Model.System { - [SugarTable("SqlDiffLog")] + [SugarTable("SqlDiffLog", "数据差异日志")] + [Tenant("0")] public class SqlDiffLog { + /// + /// 主键 + /// [SugarColumn(IsPrimaryKey = true)] + [JsonConverter(typeof(ValueToStringConverter))] public long PId { get; set; } + /// + /// 表名 + /// public string TableName { get; set; } + /// + /// 业务数据内容 + /// [SugarColumn(Length = 4000)] - public object BusinessData { get; set; } + public string BusinessData { get; set; } + /// + /// 差异类型insert,update,delete + /// public string DiffType { get; set; } + /// + /// 执行sql语句 + /// [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)] public string Sql { get; set; } + /// + /// 变更前数据 + /// [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)] public string BeforeData { get; set; } + /// + /// 变更后数据 + /// [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)] public string AfterData { get; set; } + /// + /// 操作用户名 + /// public string UserName { get; set; } public DateTime AddTime { get; set; } + /// + /// 数据库配置id + /// + public string ConfigId { get; set; } } } diff --git a/ZR.Model/System/SysDept.cs b/ZR.Model/System/SysDept.cs index e864b30..1817f13 100644 --- a/ZR.Model/System/SysDept.cs +++ b/ZR.Model/System/SysDept.cs @@ -1,7 +1,4 @@ -using SqlSugar; -using System.Collections.Generic; - -namespace ZR.Model.System +namespace ZR.Model.System { /// /// 部门表 @@ -13,7 +10,8 @@ namespace ZR.Model.System /// /// 部门ID /// - [SqlSugar.SugarColumn(IsIdentity = true, IsPrimaryKey = true)] + [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] + [JsonConverter(typeof(ValueToStringConverter))] public long DeptId { get; set; } /// @@ -58,13 +56,13 @@ namespace ZR.Model.System /// /// 部门状态:0正常,1停用 /// - [SugarColumn(Length = 1, DefaultValue = "0")] + [SugarColumn(DefaultValue = "0")] public int Status { get; set; } /// /// 删除标志(0代表存在 2代表删除) /// - [SugarColumn(Length = 1, DefaultValue = "0")] + [SugarColumn(DefaultValue = "0")] public int DelFlag { get; set; } ///