From 62ccea43607111ae516f213b7a92c838582ad5ad Mon Sep 17 00:00:00 2001 From: Elivo Date: Tue, 3 Feb 2026 11:20:11 +0800 Subject: [PATCH] =?UTF-8?q?[Column]=20=E5=92=8C=20[Table]=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89=E5=91=BD=E5=90=8D=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/Source/ColumnAttribute.cs | 16 +++++++++++++++- Apewer/Source/TableAttribute.cs | 21 ++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Apewer/Source/ColumnAttribute.cs b/Apewer/Source/ColumnAttribute.cs index f0938b3..d8f43b5 100644 --- a/Apewer/Source/ColumnAttribute.cs +++ b/Apewer/Source/ColumnAttribute.cs @@ -176,7 +176,18 @@ namespace Apewer.Source if (nu) ca._noupdate = true; // 检查列名称。 - if (TextUtility.IsBlank(ca.Field)) ca._field = property.Name; + if (TextUtility.IsBlank(ca.Field)) + { + if (CustomField == null) + { + ca._field = property.Name; + } + else + { + ca._field = CustomField.Invoke(property); + if (string.IsNullOrEmpty(ca._field)) throw new ArgumentException($"CustomField 未返回属性 {property.DeclaringType.Name}.{property.Name} 的字段。"); + } + } // 类型兼容。 if (pt.Equals(typeof(byte[]))) ca._type = ColumnType.Bytes; @@ -265,6 +276,9 @@ namespace Apewer.Source internal void SetPrimaryKey() => _primarykey = true; + /// 根据属性自定义字段。 + public static Func CustomField { get; set; } + } } diff --git a/Apewer/Source/TableAttribute.cs b/Apewer/Source/TableAttribute.cs index 2fd55b1..3b9a696 100644 --- a/Apewer/Source/TableAttribute.cs +++ b/Apewer/Source/TableAttribute.cs @@ -65,6 +65,13 @@ namespace Apewer.Source #endregion + #region 自定义 + + /// 根据类型自定义表名。 + public static Func CustomTableName { get; set; } + + #endregion + #region cache private static Dictionary _tac = new Dictionary(); @@ -106,7 +113,19 @@ namespace Apewer.Source } ta._model = type; - if (string.IsNullOrEmpty(ta.Name)) ta._name = type.Name; + if (string.IsNullOrEmpty(ta.Name)) + { + if (CustomTableName == null) + { + ta._name = type.Name; + } + else + { + ta._name = CustomTableName.Invoke(type); + if (string.IsNullOrEmpty(ta._name)) throw new ArgumentException($"CustomTableName 未返回类型 {type.Name} 的表名。"); + } + + } ta.Independent = RuntimeUtility.Contains(type, true); ta._primarykey = RuntimeUtility.IsInherits(type, InterfacePrimaryKey);