- 增加 UseMappingPriority 指定映射优先级;#387 #69 #99

This commit is contained in:
2881099 2022-06-01 09:38:59 +08:00
parent 184d11d692
commit 7290109734
4 changed files with 399 additions and 227 deletions

View File

@ -1413,6 +1413,24 @@
<param name="convertType"></param> <param name="convertType"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:FreeSql.FreeSqlBuilder.UseMappingPriority(FreeSql.Internal.MappingPriorityType,FreeSql.Internal.MappingPriorityType,FreeSql.Internal.MappingPriorityType)">
<summary>
指定映射优先级<para></para>
例如表名:实体类名 &lt; Aop &lt; FluentApi &lt; Attribute &lt; AsTable<para></para>
事件 Aop -------> fsql.Aop.ConfigEntity/fsql.Aop.ConfigEntityProperty<para></para>
方法 FluentApi -> fsql.CodeFirst.ConfigEntity/fsql.CodeFirst.Entity<para></para>
特性 Attribute -> [Table(Name = xxx, ...)]<para></para>
-----------------------------------------------------------------------------<para></para>
默认规则关于映射优先级Attribute 可以更直观排查问题,即使任何地方使用 FluentApi/Aop 设置 TableName 都不生效。<para></para>
调整规则UseMappingPriority(Attribute, FluentApi, Aop) <para></para>
实体类名 &lt; Attribute &lt; FluentApi &lt; Aop &lt; AsTable
</summary>
<param name="mappingType1"></param>
<param name="mappingType2"></param>
<param name="mappingType3"></param>
<returns></returns>
<exception cref="T:System.ArgumentException"></exception>
</member>
<member name="M:FreeSql.FreeSqlBuilder.UseExitAutoDisposePool(System.Boolean)"> <member name="M:FreeSql.FreeSqlBuilder.UseExitAutoDisposePool(System.Boolean)">
<summary> <summary>
监听 AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress 事件自动释放连接池<para></para> 监听 AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress 事件自动释放连接池<para></para>
@ -4037,6 +4055,112 @@
更新的实体 更新的实体
</summary> </summary>
</member> </member>
<member name="T:FreeSql.Internal.MappingPriorityType">
<summary>
映射优先级,默认: Attribute > FluentApi > Aop
</summary>
</member>
<member name="F:FreeSql.Internal.MappingPriorityType.Attribute">
<summary>
实体特性<para></para>
[Table(Name = "tabname")]<para></para>
[Column(Name = "table_id")]
</summary>
</member>
<member name="F:FreeSql.Internal.MappingPriorityType.FluentApi">
<summary>
流式接口<para></para>
fsql.CodeFirst.ConfigEntity(a => a.Name("tabname"))<para></para>
fsql.CodeFirst.ConfigEntity(a => a.Property(b => b.Id).Name("table_id"))
</summary>
</member>
<member name="F:FreeSql.Internal.MappingPriorityType.Aop">
<summary>
AOP 特性 https://github.com/dotnetcore/FreeSql/wiki/AOP<para></para>
fsql.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = "public.tabname";<para></para>
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = "table_id";<para></para>
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.None">
<summary>
不进行任何处理
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.PascalCaseToUnderscore">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串
<para></para>
BigApple -> Big_Apple
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.PascalCaseToUnderscoreWithUpper">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写
<para></para>
BigApple -> BIG_APPLE
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.PascalCaseToUnderscoreWithLower">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写
<para></para>
BigApple -> big_apple
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.ToUpper">
<summary>
将字符串转换为大写
<para></para>
BigApple -> BIGAPPLE
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.ToLower">
<summary>
将字符串转换为小写
<para></para>
BigApple -> bigapple
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.None">
<summary>
不进行任何处理
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.PascalCaseToUnderscore">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串
<para></para>
BigApple -> Big_Apple
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.PascalCaseToUnderscoreWithUpper">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写
<para></para>
BigApple -> BIG_APPLE
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.PascalCaseToUnderscoreWithLower">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写
<para></para>
BigApple -> big_apple
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.Upper">
<summary>
将字符串转换为大写
<para></para>
BigApple -> BIGAPPLE
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.Lower">
<summary>
将字符串转换为小写
<para></para>
BigApple -> bigapple
</summary>
</member>
<member name="M:FreeSql.Internal.GlobalFilter.Apply``1(System.String,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})"> <member name="M:FreeSql.Internal.GlobalFilter.Apply``1(System.String,System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
<summary> <summary>
创建一个过滤器<para></para> 创建一个过滤器<para></para>
@ -4336,6 +4460,7 @@
将对象池设置为不可用,后续 Get/GetAsync 均会报错,同时启动后台定时检查服务恢复可用 将对象池设置为不可用,后续 Get/GetAsync 均会报错,同时启动后台定时检查服务恢复可用
</summary> </summary>
<param name="exception"></param> <param name="exception"></param>
<param name="lastGetTime"></param>
<returns>由【可用】变成【不可用】时返回true否则返回false</returns> <returns>由【可用】变成【不可用】时返回true否则返回false</returns>
</member> </member>
<member name="P:FreeSql.Internal.ObjectPool.IObjectPool`1.Statistics"> <member name="P:FreeSql.Internal.ObjectPool.IObjectPool`1.Statistics">
@ -4545,86 +4670,6 @@
</summary> </summary>
<returns></returns> <returns></returns>
</member> </member>
<member name="F:FreeSql.Internal.StringConvertType.None">
<summary>
不进行任何处理
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.PascalCaseToUnderscore">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串
<para></para>
BigApple -> Big_Apple
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.PascalCaseToUnderscoreWithUpper">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写
<para></para>
BigApple -> BIG_APPLE
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.PascalCaseToUnderscoreWithLower">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写
<para></para>
BigApple -> big_apple
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.Upper">
<summary>
将字符串转换为大写
<para></para>
BigApple -> BIGAPPLE
</summary>
</member>
<member name="F:FreeSql.Internal.StringConvertType.Lower">
<summary>
将字符串转换为小写
<para></para>
BigApple -> bigapple
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.None">
<summary>
不进行任何处理
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.PascalCaseToUnderscore">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串
<para></para>
BigApple -> Big_Apple
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.PascalCaseToUnderscoreWithUpper">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写
<para></para>
BigApple -> BIG_APPLE
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.PascalCaseToUnderscoreWithLower">
<summary>
将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写
<para></para>
BigApple -> big_apple
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.ToUpper">
<summary>
将字符串转换为大写
<para></para>
BigApple -> BIGAPPLE
</summary>
</member>
<member name="F:FreeSql.Internal.NameConvertType.ToLower">
<summary>
将字符串转换为小写
<para></para>
BigApple -> bigapple
</summary>
</member>
<member name="T:FreeSql.CoreStrings"> <member name="T:FreeSql.CoreStrings">
<summary> <summary>
<para> <para>

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using FreeSql.DataAnnotations; using FreeSql.DataAnnotations;
using FreeSql.Internal; using FreeSql.Internal;
using FreeSql.Internal.CommonProvider;
namespace FreeSql namespace FreeSql
{ {
@ -24,6 +25,7 @@ namespace FreeSql
bool _isGenerateCommandParameterWithLambda = false; bool _isGenerateCommandParameterWithLambda = false;
bool _isLazyLoading = false; bool _isLazyLoading = false;
bool _isExitAutoDisposePool = true; bool _isExitAutoDisposePool = true;
MappingPriorityType[] _mappingPriorityTypes;
StringConvertType _entityPropertyConvertType = StringConvertType.None; StringConvertType _entityPropertyConvertType = StringConvertType.None;
NameConvertType _nameConvertType = NameConvertType.None; NameConvertType _nameConvertType = NameConvertType.None;
Action<DbCommand> _aopCommandExecuting = null; Action<DbCommand> _aopCommandExecuting = null;
@ -160,6 +162,29 @@ namespace FreeSql
return this; return this;
} }
/// <summary>
/// 指定映射优先级<para></para>
/// 例如表名:实体类名 &lt; Aop &lt; FluentApi &lt; Attribute &lt; AsTable<para></para>
/// 事件 Aop -------> fsql.Aop.ConfigEntity/fsql.Aop.ConfigEntityProperty<para></para>
/// 方法 FluentApi -> fsql.CodeFirst.ConfigEntity/fsql.CodeFirst.Entity<para></para>
/// 特性 Attribute -> [Table(Name = xxx, ...)]<para></para>
/// -----------------------------------------------------------------------------<para></para>
/// 默认规则关于映射优先级Attribute 可以更直观排查问题,即使任何地方使用 FluentApi/Aop 设置 TableName 都不生效。<para></para>
/// 调整规则UseMappingPriority(Attribute, FluentApi, Aop) <para></para>
/// 实体类名 &lt; Attribute &lt; FluentApi &lt; Aop &lt; AsTable
/// </summary>
/// <param name="mappingType1"></param>
/// <param name="mappingType2"></param>
/// <param name="mappingType3"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public FreeSqlBuilder UseMappingPriority(MappingPriorityType mappingType1, MappingPriorityType mappingType2, MappingPriorityType mappingType3)
{
if (mappingType1 == mappingType2 || mappingType1 == mappingType3 || mappingType2 == mappingType3) throw new ArgumentException($"{nameof(mappingType1)}、{nameof(mappingType2)}、{nameof(mappingType3)} 不可以相等");
_mappingPriorityTypes = new[] { mappingType1, mappingType2, mappingType3 };
return this;
}
/// <summary> /// <summary>
/// 监听 AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress 事件自动释放连接池<para></para> /// 监听 AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress 事件自动释放连接池<para></para>
/// 默认值: true /// 默认值: true
@ -298,6 +323,9 @@ namespace FreeSql
ret.CodeFirst.IsGenerateCommandParameterWithLambda = _isGenerateCommandParameterWithLambda; ret.CodeFirst.IsGenerateCommandParameterWithLambda = _isGenerateCommandParameterWithLambda;
ret.CodeFirst.IsLazyLoading = _isLazyLoading; ret.CodeFirst.IsLazyLoading = _isLazyLoading;
if (_mappingPriorityTypes != null)
(ret.Select<object>() as Select0Provider)._commonUtils._mappingPriorityTypes = _mappingPriorityTypes;
if (_aopCommandExecuting != null) if (_aopCommandExecuting != null)
ret.Aop.CommandBefore += new EventHandler<Aop.CommandBeforeEventArgs>((s, e) => _aopCommandExecuting?.Invoke(e.Command)); ret.Aop.CommandBefore += new EventHandler<Aop.CommandBeforeEventArgs>((s, e) => _aopCommandExecuting?.Invoke(e.Command));
if (_aopCommandExecuted != null) if (_aopCommandExecuted != null)

View File

@ -111,24 +111,37 @@ namespace FreeSql.Internal
{ {
return dicConfigEntity.TryGetValue(type, out var trytb) ? trytb : null; return dicConfigEntity.TryGetValue(type, out var trytb) ? trytb : null;
} }
public MappingPriorityType[] _mappingPriorityTypes = new[] { MappingPriorityType.Aop, MappingPriorityType.FluentApi, MappingPriorityType.Attribute };
public TableAttribute GetEntityTableAttribute(Type type) public TableAttribute GetEntityTableAttribute(Type type)
{ {
TableAttribute attr = null; var attr = new TableAttribute();
foreach (var mp in _mappingPriorityTypes)
{
switch (mp)
{
case MappingPriorityType.Aop:
if (_orm.Aop.ConfigEntityHandler != null) if (_orm.Aop.ConfigEntityHandler != null)
{ {
var aope = new Aop.ConfigEntityEventArgs(type); var aope = new Aop.ConfigEntityEventArgs(type);
_orm.Aop.ConfigEntityHandler(_orm, aope); _orm.Aop.ConfigEntityHandler(_orm, aope);
attr = aope.ModifyResult; var tryattr = aope.ModifyResult;
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
if (tryattr._DisableSyncStructure != null) attr._DisableSyncStructure = tryattr.DisableSyncStructure;
if (!string.IsNullOrEmpty(tryattr.AsTable)) attr.AsTable = tryattr.AsTable;
} }
if (attr == null) attr = new TableAttribute(); break;
case MappingPriorityType.FluentApi:
if (dicConfigEntity.TryGetValue(type, out var trytb)) if (dicConfigEntity.TryGetValue(type, out var trytb))
{ {
if (!string.IsNullOrEmpty(trytb.Name)) attr.Name = trytb.Name; if (!string.IsNullOrEmpty(trytb.Name)) attr.Name = trytb.Name;
if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName; if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName;
if (trytb._DisableSyncStructure != null) attr._DisableSyncStructure = trytb.DisableSyncStructure; if (trytb._DisableSyncStructure != null) attr._DisableSyncStructure = trytb.DisableSyncStructure;
if (!string.IsNullOrEmpty(trytb.AsTable)) attr.AsTable = trytb.AsTable; if (!string.IsNullOrEmpty(trytb.AsTable)) attr.AsTable = trytb.AsTable;
} }
break;
case MappingPriorityType.Attribute:
var attrs = type.GetCustomAttributes(typeof(TableAttribute), false); var attrs = type.GetCustomAttributes(typeof(TableAttribute), false);
foreach (var tryattrobj in attrs) foreach (var tryattrobj in attrs)
{ {
@ -139,6 +152,9 @@ namespace FreeSql.Internal
if (tryattr._DisableSyncStructure != null) attr._DisableSyncStructure = tryattr.DisableSyncStructure; if (tryattr._DisableSyncStructure != null) attr._DisableSyncStructure = tryattr.DisableSyncStructure;
if (!string.IsNullOrEmpty(tryattr.AsTable)) attr.AsTable = tryattr.AsTable; if (!string.IsNullOrEmpty(tryattr.AsTable)) attr.AsTable = tryattr.AsTable;
} }
break;
}
}
if (!string.IsNullOrEmpty(attr.Name)) return attr; if (!string.IsNullOrEmpty(attr.Name)) return attr;
if (!string.IsNullOrEmpty(attr.OldName)) return attr; if (!string.IsNullOrEmpty(attr.OldName)) return attr;
if (attr._DisableSyncStructure != null) return attr; if (attr._DisableSyncStructure != null) return attr;
@ -147,14 +163,39 @@ namespace FreeSql.Internal
} }
public ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) public ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto)
{ {
ColumnAttribute attr = null; var attr = new ColumnAttribute();
foreach (var mp in _mappingPriorityTypes)
{
switch (mp)
{
case MappingPriorityType.Aop:
if (_orm.Aop.ConfigEntityPropertyHandler != null) if (_orm.Aop.ConfigEntityPropertyHandler != null)
{ {
var aope = new Aop.ConfigEntityPropertyEventArgs(type, proto); var aope = new Aop.ConfigEntityPropertyEventArgs(type, proto);
_orm.Aop.ConfigEntityPropertyHandler(_orm, aope); _orm.Aop.ConfigEntityPropertyHandler(_orm, aope);
attr = aope.ModifyResult; var tryattr = aope.ModifyResult;
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
if (!string.IsNullOrEmpty(tryattr.DbType)) attr.DbType = tryattr.DbType;
if (tryattr._IsPrimary != null) attr._IsPrimary = tryattr.IsPrimary;
if (tryattr._IsIdentity != null) attr._IsIdentity = tryattr.IsIdentity;
if (tryattr._IsNullable != null) attr._IsNullable = tryattr.IsNullable;
if (tryattr._IsIgnore != null) attr._IsIgnore = tryattr.IsIgnore;
if (tryattr._IsVersion != null) attr._IsVersion = tryattr.IsVersion;
if (tryattr.MapType != null) attr.MapType = tryattr.MapType;
if (tryattr._Position != null) attr._Position = tryattr.Position;
if (tryattr._CanInsert != null) attr._CanInsert = tryattr.CanInsert;
if (tryattr._CanUpdate != null) attr._CanUpdate = tryattr.CanUpdate;
if (tryattr.ServerTime != DateTimeKind.Unspecified) attr.ServerTime = tryattr.ServerTime;
if (tryattr._StringLength != null) attr.StringLength = tryattr.StringLength;
if (!string.IsNullOrEmpty(tryattr.InsertValueSql)) attr.InsertValueSql = tryattr.InsertValueSql;
if (tryattr._Precision != null) attr.Precision = tryattr.Precision;
if (tryattr._Scale != null) attr.Scale = tryattr.Scale;
if (!string.IsNullOrEmpty(tryattr.RewriteSql)) attr.RewriteSql = tryattr.RewriteSql;
if (!string.IsNullOrEmpty(tryattr.RereadSql)) attr.RereadSql = tryattr.RereadSql;
} }
if (attr == null) attr = new ColumnAttribute(); break;
case MappingPriorityType.FluentApi:
if (dicConfigEntity.TryGetValue(type, out var trytb) && trytb._columns.TryGetValue(proto.Name, out var trycol)) if (dicConfigEntity.TryGetValue(type, out var trytb) && trytb._columns.TryGetValue(proto.Name, out var trycol))
{ {
if (!string.IsNullOrEmpty(trycol.Name)) attr.Name = trycol.Name; if (!string.IsNullOrEmpty(trycol.Name)) attr.Name = trycol.Name;
@ -177,6 +218,8 @@ namespace FreeSql.Internal
if (!string.IsNullOrEmpty(trycol.RewriteSql)) attr.RewriteSql = trycol.RewriteSql; if (!string.IsNullOrEmpty(trycol.RewriteSql)) attr.RewriteSql = trycol.RewriteSql;
if (!string.IsNullOrEmpty(trycol.RereadSql)) attr.RereadSql = trycol.RereadSql; if (!string.IsNullOrEmpty(trycol.RereadSql)) attr.RereadSql = trycol.RereadSql;
} }
break;
case MappingPriorityType.Attribute:
var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false); var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false);
foreach (var tryattrobj in attrs) foreach (var tryattrobj in attrs)
{ {
@ -202,6 +245,9 @@ namespace FreeSql.Internal
if (!string.IsNullOrEmpty(tryattr.RewriteSql)) attr.RewriteSql = tryattr.RewriteSql; if (!string.IsNullOrEmpty(tryattr.RewriteSql)) attr.RewriteSql = tryattr.RewriteSql;
if (!string.IsNullOrEmpty(tryattr.RereadSql)) attr.RereadSql = tryattr.RereadSql; if (!string.IsNullOrEmpty(tryattr.RereadSql)) attr.RereadSql = tryattr.RereadSql;
} }
break;
}
}
ColumnAttribute ret = null; ColumnAttribute ret = null;
if (!string.IsNullOrEmpty(attr.Name)) ret = attr; if (!string.IsNullOrEmpty(attr.Name)) ret = attr;
if (!string.IsNullOrEmpty(attr.OldName)) ret = attr; if (!string.IsNullOrEmpty(attr.OldName)) ret = attr;
@ -228,11 +274,21 @@ namespace FreeSql.Internal
public NavigateAttribute GetEntityNavigateAttribute(Type type, PropertyInfo proto) public NavigateAttribute GetEntityNavigateAttribute(Type type, PropertyInfo proto)
{ {
var attr = new NavigateAttribute(); var attr = new NavigateAttribute();
foreach (var mp in _mappingPriorityTypes)
{
switch (mp)
{
case MappingPriorityType.Aop:
break;
case MappingPriorityType.FluentApi:
if (dicConfigEntity.TryGetValue(type, out var trytb) && trytb._navigates.TryGetValue(proto.Name, out var trynav)) if (dicConfigEntity.TryGetValue(type, out var trytb) && trytb._navigates.TryGetValue(proto.Name, out var trynav))
{ {
if (!string.IsNullOrEmpty(trynav.Bind)) attr.Bind = trynav.Bind; if (!string.IsNullOrEmpty(trynav.Bind)) attr.Bind = trynav.Bind;
if (trynav.ManyToMany != null) attr.ManyToMany = trynav.ManyToMany; if (trynav.ManyToMany != null) attr.ManyToMany = trynav.ManyToMany;
} }
break;
case MappingPriorityType.Attribute:
var attrs = proto.GetCustomAttributes(typeof(NavigateAttribute), false); var attrs = proto.GetCustomAttributes(typeof(NavigateAttribute), false);
foreach (var tryattrobj in attrs) foreach (var tryattrobj in attrs)
{ {
@ -241,6 +297,9 @@ namespace FreeSql.Internal
if (!string.IsNullOrEmpty(trynav.Bind)) attr.Bind = trynav.Bind; if (!string.IsNullOrEmpty(trynav.Bind)) attr.Bind = trynav.Bind;
if (trynav.ManyToMany != null) attr.ManyToMany = trynav.ManyToMany; if (trynav.ManyToMany != null) attr.ManyToMany = trynav.ManyToMany;
} }
break;
}
}
NavigateAttribute ret = null; NavigateAttribute ret = null;
if (!string.IsNullOrEmpty(attr.Bind)) ret = attr; if (!string.IsNullOrEmpty(attr.Bind)) ret = attr;
if (attr.ManyToMany != null) ret = attr; if (attr.ManyToMany != null) ret = attr;
@ -249,6 +308,11 @@ namespace FreeSql.Internal
public IndexAttribute[] GetEntityIndexAttribute(Type type) public IndexAttribute[] GetEntityIndexAttribute(Type type)
{ {
var ret = new Dictionary<string, IndexAttribute>(); var ret = new Dictionary<string, IndexAttribute>();
foreach (var mp in _mappingPriorityTypes)
{
switch (mp)
{
case MappingPriorityType.Aop:
if (_orm.Aop.ConfigEntityHandler != null) if (_orm.Aop.ConfigEntityHandler != null)
{ {
var aope = new Aop.ConfigEntityEventArgs(type); var aope = new Aop.ConfigEntityEventArgs(type);
@ -260,6 +324,8 @@ namespace FreeSql.Internal
ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
} }
} }
break;
case MappingPriorityType.FluentApi:
if (dicConfigEntity.TryGetValue(type, out var trytb)) if (dicConfigEntity.TryGetValue(type, out var trytb))
{ {
foreach (var idxattr in trytb._indexs.Values) foreach (var idxattr in trytb._indexs.Values)
@ -269,6 +335,8 @@ namespace FreeSql.Internal
ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
} }
} }
break;
case MappingPriorityType.Attribute:
var attrs = type.GetCustomAttributes(typeof(IndexAttribute), true); var attrs = type.GetCustomAttributes(typeof(IndexAttribute), true);
foreach (var tryattrobj in attrs) foreach (var tryattrobj in attrs)
{ {
@ -280,6 +348,9 @@ namespace FreeSql.Internal
ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
} }
} }
break;
}
}
return ret.Values.ToArray(); return ret.Values.ToArray();
} }

View File

@ -2,47 +2,31 @@
namespace FreeSql.Internal namespace FreeSql.Internal
{ {
public enum StringConvertType /// <summary>
/// 映射优先级,默认: Attribute > FluentApi > Aop
/// </summary>
public enum MappingPriorityType
{ {
/// <summary> /// <summary>
/// 不进行任何处理 /// 实体特性<para></para>
/// [Table(Name = "tabname")]<para></para>
/// [Column(Name = "table_id")]
/// </summary> /// </summary>
None = 0, Attribute = 0,
/// <summary> /// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串 /// 流式接口<para></para>
/// <para></para> /// fsql.CodeFirst.ConfigEntity(a => a.Name("tabname"))<para></para>
/// BigApple -> Big_Apple /// fsql.CodeFirst.ConfigEntity(a => a.Property(b => b.Id).Name("table_id"))
/// </summary> /// </summary>
PascalCaseToUnderscore, FluentApi,
/// <summary> /// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写 /// AOP 特性 https://github.com/dotnetcore/FreeSql/wiki/AOP<para></para>
/// <para></para> /// fsql.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = "public.tabname";<para></para>
/// BigApple -> BIG_APPLE /// fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = "table_id";<para></para>
/// </summary> /// </summary>
PascalCaseToUnderscoreWithUpper, Aop
/// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写
/// <para></para>
/// BigApple -> big_apple
/// </summary>
PascalCaseToUnderscoreWithLower,
/// <summary>
/// 将字符串转换为大写
/// <para></para>
/// BigApple -> BIGAPPLE
/// </summary>
Upper,
/// <summary>
/// 将字符串转换为小写
/// <para></para>
/// BigApple -> bigapple
/// </summary>
Lower
} }
public enum NameConvertType public enum NameConvertType
@ -94,4 +78,48 @@ namespace FreeSql.Internal
/// </summary> /// </summary>
ToLower ToLower
} }
public enum StringConvertType
{
/// <summary>
/// 不进行任何处理
/// </summary>
None = 0,
/// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串
/// <para></para>
/// BigApple -> Big_Apple
/// </summary>
PascalCaseToUnderscore,
/// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写
/// <para></para>
/// BigApple -> BIG_APPLE
/// </summary>
PascalCaseToUnderscoreWithUpper,
/// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写
/// <para></para>
/// BigApple -> big_apple
/// </summary>
PascalCaseToUnderscoreWithLower,
/// <summary>
/// 将字符串转换为大写
/// <para></para>
/// BigApple -> BIGAPPLE
/// </summary>
Upper,
/// <summary>
/// 将字符串转换为小写
/// <para></para>
/// BigApple -> bigapple
/// </summary>
Lower
}
} }