Browse Source

[Column] 和 [Table] 增加自定义命名的功能。

master
Elivo 3 months ago
parent
commit
62ccea4360
  1. 16
      Apewer/Source/ColumnAttribute.cs
  2. 21
      Apewer/Source/TableAttribute.cs

16
Apewer/Source/ColumnAttribute.cs

@ -176,7 +176,18 @@ namespace Apewer.Source
if (nu) ca._noupdate = true; 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; if (pt.Equals(typeof(byte[]))) ca._type = ColumnType.Bytes;
@ -265,6 +276,9 @@ namespace Apewer.Source
internal void SetPrimaryKey() => _primarykey = true; internal void SetPrimaryKey() => _primarykey = true;
/// <summary>根据属性自定义字段。</summary>
public static Func<PropertyInfo, string> CustomField { get; set; }
} }
} }

21
Apewer/Source/TableAttribute.cs

@ -65,6 +65,13 @@ namespace Apewer.Source
#endregion #endregion
#region 自定义
/// <summary>根据类型自定义表名。</summary>
public static Func<Type, string> CustomTableName { get; set; }
#endregion
#region cache #region cache
private static Dictionary<string, TableAttribute> _tac = new Dictionary<string, TableAttribute>(); private static Dictionary<string, TableAttribute> _tac = new Dictionary<string, TableAttribute>();
@ -106,7 +113,19 @@ namespace Apewer.Source
} }
ta._model = type; 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<IndependentAttribute>(type, true); ta.Independent = RuntimeUtility.Contains<IndependentAttribute>(type, true);
ta._primarykey = RuntimeUtility.IsInherits(type, InterfacePrimaryKey); ta._primarykey = RuntimeUtility.IsInherits(type, InterfacePrimaryKey);

Loading…
Cancel
Save