瀏覽代碼

自动映射库名.表名,方便跨库增删查改

Tony 3 年之前
父節點
當前提交
cd6214a875
共有 2 個文件被更改,包括 15 次插入0 次删除
  1. 5 0
      Admin.NET/Admin.NET.Core/Const/SqlSugarConst.cs
  2. 10 0
      Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

+ 5 - 0
Admin.NET/Admin.NET.Core/Const/SqlSugarConst.cs

@@ -19,4 +19,9 @@ public class SqlSugarConst
     /// SqlSugar数据库链接集合(多库代码生成用)
     /// </summary>
     public static List<ConnectionConfig> ConnectionConfigs = new();
+
+    /// <summary>
+    /// 默认库名  DefaultConnection 对应的库名
+    /// </summary>
+    public const string CONFIG_DEFAULT_DB = "Dilon";
 }

+ 10 - 0
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -27,6 +27,16 @@ public static class SqlSugarSetup
                         || (type.PropertyType == typeof(string) && type.GetCustomAttribute<RequiredAttribute>() == null))
                         column.IsNullable = true;
                 },
+                EntityNameService = (type, entity) =>
+                {
+                    //实体类映射库名.表名
+                    var attr = type.GetCustomAttribute<SqlSugarEntityAttribute>();
+                    if (attr == null) return;
+                    var configId = attr.DbConfigId == SqlSugarConst.ConfigId ? SqlSugarConst.CONFIG_DEFAULT_DB : attr.DbConfigId;
+                    var tableName = type.GetCustomAttribute<SugarTable>().TableName;
+                    entity.DbTableName = $"{configId}.{tableName}";
+                    Console.WriteLine("\r\n" + type.Name.PadRight(30, ' ') + "===映射===>     " + entity.DbTableName);
+                }
             };
             var defaultConnection = new ConnectionConfig()
             {