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);