From 72901097341d0a0f4e02b38c7c94f362a606817e Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Wed, 1 Jun 2022 09:38:59 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20UseMappingPriority=20?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=98=A0=E5=B0=84=E4=BC=98=E5=85=88=E7=BA=A7?= =?UTF-8?q?=EF=BC=9B#387=20#69=20#99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql/FreeSql.xml | 205 +++++++----- FreeSql/FreeSqlBuilder.cs | 28 ++ FreeSql/Internal/CommonUtils.cs | 301 +++++++++++------- ...gConvertType.cs => FreeSqlBuilderTypes.cs} | 92 ++++-- 4 files changed, 399 insertions(+), 227 deletions(-) rename FreeSql/Internal/{StringConvertType.cs => FreeSqlBuilderTypes.cs} (73%) diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 995ea24d..2db205b7 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -1413,6 +1413,24 @@ + + + 指定映射优先级 + 例如表名:实体类名 < Aop < FluentApi < Attribute < AsTable + 事件 Aop -------> fsql.Aop.ConfigEntity/fsql.Aop.ConfigEntityProperty + 方法 FluentApi -> fsql.CodeFirst.ConfigEntity/fsql.CodeFirst.Entity + 特性 Attribute -> [Table(Name = xxx, ...)] + ----------------------------------------------------------------------------- + 默认规则:关于映射优先级,Attribute 可以更直观排查问题,即使任何地方使用 FluentApi/Aop 设置 TableName 都不生效。 + 调整规则:UseMappingPriority(Attribute, FluentApi, Aop) + 实体类名 < Attribute < FluentApi < Aop < AsTable + + + + + + + 监听 AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress 事件自动释放连接池 @@ -4037,6 +4055,112 @@ 更新的实体 + + + 映射优先级,默认: Attribute > FluentApi > Aop + + + + + 实体特性 + [Table(Name = "tabname")] + [Column(Name = "table_id")] + + + + + 流式接口 + fsql.CodeFirst.ConfigEntity(a => a.Name("tabname")) + fsql.CodeFirst.ConfigEntity(a => a.Property(b => b.Id).Name("table_id")) + + + + + AOP 特性 https://github.com/dotnetcore/FreeSql/wiki/AOP + fsql.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = "public.tabname"; + fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = "table_id"; + + + + + 不进行任何处理 + + + + + 将帕斯卡命名字符串转换为下划线分隔字符串 + + BigApple -> Big_Apple + + + + + 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写 + + BigApple -> BIG_APPLE + + + + + 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写 + + BigApple -> big_apple + + + + + 将字符串转换为大写 + + BigApple -> BIGAPPLE + + + + + 将字符串转换为小写 + + BigApple -> bigapple + + + + + 不进行任何处理 + + + + + 将帕斯卡命名字符串转换为下划线分隔字符串 + + BigApple -> Big_Apple + + + + + 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写 + + BigApple -> BIG_APPLE + + + + + 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写 + + BigApple -> big_apple + + + + + 将字符串转换为大写 + + BigApple -> BIGAPPLE + + + + + 将字符串转换为小写 + + BigApple -> bigapple + + 创建一个过滤器 @@ -4336,6 +4460,7 @@ 将对象池设置为不可用,后续 Get/GetAsync 均会报错,同时启动后台定时检查服务恢复可用 + 由【可用】变成【不可用】时返回true,否则返回false @@ -4545,86 +4670,6 @@ - - - 不进行任何处理 - - - - - 将帕斯卡命名字符串转换为下划线分隔字符串 - - BigApple -> Big_Apple - - - - - 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写 - - BigApple -> BIG_APPLE - - - - - 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写 - - BigApple -> big_apple - - - - - 将字符串转换为大写 - - BigApple -> BIGAPPLE - - - - - 将字符串转换为小写 - - BigApple -> bigapple - - - - - 不进行任何处理 - - - - - 将帕斯卡命名字符串转换为下划线分隔字符串 - - BigApple -> Big_Apple - - - - - 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写 - - BigApple -> BIG_APPLE - - - - - 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写 - - BigApple -> big_apple - - - - - 将字符串转换为大写 - - BigApple -> BIGAPPLE - - - - - 将字符串转换为小写 - - BigApple -> bigapple - - diff --git a/FreeSql/FreeSqlBuilder.cs b/FreeSql/FreeSqlBuilder.cs index ef5feba6..df8f3bc7 100644 --- a/FreeSql/FreeSqlBuilder.cs +++ b/FreeSql/FreeSqlBuilder.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Reflection; using FreeSql.DataAnnotations; using FreeSql.Internal; +using FreeSql.Internal.CommonProvider; namespace FreeSql { @@ -24,6 +25,7 @@ namespace FreeSql bool _isGenerateCommandParameterWithLambda = false; bool _isLazyLoading = false; bool _isExitAutoDisposePool = true; + MappingPriorityType[] _mappingPriorityTypes; StringConvertType _entityPropertyConvertType = StringConvertType.None; NameConvertType _nameConvertType = NameConvertType.None; Action _aopCommandExecuting = null; @@ -160,6 +162,29 @@ namespace FreeSql return this; } + /// + /// 指定映射优先级 + /// 例如表名:实体类名 < Aop < FluentApi < Attribute < AsTable + /// 事件 Aop -------> fsql.Aop.ConfigEntity/fsql.Aop.ConfigEntityProperty + /// 方法 FluentApi -> fsql.CodeFirst.ConfigEntity/fsql.CodeFirst.Entity + /// 特性 Attribute -> [Table(Name = xxx, ...)] + /// ----------------------------------------------------------------------------- + /// 默认规则:关于映射优先级,Attribute 可以更直观排查问题,即使任何地方使用 FluentApi/Aop 设置 TableName 都不生效。 + /// 调整规则:UseMappingPriority(Attribute, FluentApi, Aop) + /// 实体类名 < Attribute < FluentApi < Aop < AsTable + /// + /// + /// + /// + /// + /// + 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; + } + /// /// 监听 AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress 事件自动释放连接池 /// 默认值: true @@ -298,6 +323,9 @@ namespace FreeSql ret.CodeFirst.IsGenerateCommandParameterWithLambda = _isGenerateCommandParameterWithLambda; ret.CodeFirst.IsLazyLoading = _isLazyLoading; + if (_mappingPriorityTypes != null) + (ret.Select() as Select0Provider)._commonUtils._mappingPriorityTypes = _mappingPriorityTypes; + if (_aopCommandExecuting != null) ret.Aop.CommandBefore += new EventHandler((s, e) => _aopCommandExecuting?.Invoke(e.Command)); if (_aopCommandExecuted != null) diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs index 443cd518..87147ac3 100644 --- a/FreeSql/Internal/CommonUtils.cs +++ b/FreeSql/Internal/CommonUtils.cs @@ -111,33 +111,49 @@ namespace FreeSql.Internal { return dicConfigEntity.TryGetValue(type, out var trytb) ? trytb : null; } + + public MappingPriorityType[] _mappingPriorityTypes = new[] { MappingPriorityType.Aop, MappingPriorityType.FluentApi, MappingPriorityType.Attribute }; public TableAttribute GetEntityTableAttribute(Type type) { - TableAttribute attr = null; - if (_orm.Aop.ConfigEntityHandler != null) + var attr = new TableAttribute(); + foreach (var mp in _mappingPriorityTypes) { - var aope = new Aop.ConfigEntityEventArgs(type); - _orm.Aop.ConfigEntityHandler(_orm, aope); - attr = aope.ModifyResult; - } - if (attr == null) attr = new TableAttribute(); - if (dicConfigEntity.TryGetValue(type, out var trytb)) - { - if (!string.IsNullOrEmpty(trytb.Name)) attr.Name = trytb.Name; - if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName; - if (trytb._DisableSyncStructure != null) attr._DisableSyncStructure = trytb.DisableSyncStructure; - if (!string.IsNullOrEmpty(trytb.AsTable)) attr.AsTable = trytb.AsTable; - - } - var attrs = type.GetCustomAttributes(typeof(TableAttribute), false); - foreach (var tryattrobj in attrs) - { - var tryattr = tryattrobj as TableAttribute; - if (tryattr == null) continue; - 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; + switch (mp) + { + case MappingPriorityType.Aop: + if (_orm.Aop.ConfigEntityHandler != null) + { + var aope = new Aop.ConfigEntityEventArgs(type); + _orm.Aop.ConfigEntityHandler(_orm, aope); + 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; + } + break; + case MappingPriorityType.FluentApi: + if (dicConfigEntity.TryGetValue(type, out var trytb)) + { + if (!string.IsNullOrEmpty(trytb.Name)) attr.Name = trytb.Name; + if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName; + if (trytb._DisableSyncStructure != null) attr._DisableSyncStructure = trytb.DisableSyncStructure; + if (!string.IsNullOrEmpty(trytb.AsTable)) attr.AsTable = trytb.AsTable; + } + break; + case MappingPriorityType.Attribute: + var attrs = type.GetCustomAttributes(typeof(TableAttribute), false); + foreach (var tryattrobj in attrs) + { + var tryattr = tryattrobj as TableAttribute; + if (tryattr == null) continue; + 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; + } + break; + } } if (!string.IsNullOrEmpty(attr.Name)) return attr; if (!string.IsNullOrEmpty(attr.OldName)) return attr; @@ -147,60 +163,90 @@ namespace FreeSql.Internal } public ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) { - ColumnAttribute attr = null; - if (_orm.Aop.ConfigEntityPropertyHandler != null) + var attr = new ColumnAttribute(); + foreach (var mp in _mappingPriorityTypes) { - var aope = new Aop.ConfigEntityPropertyEventArgs(type, proto); - _orm.Aop.ConfigEntityPropertyHandler(_orm, aope); - attr = aope.ModifyResult; - } - if (attr == null) attr = new ColumnAttribute(); - 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.OldName)) attr.OldName = trycol.OldName; - if (!string.IsNullOrEmpty(trycol.DbType)) attr.DbType = trycol.DbType; - if (trycol._IsPrimary != null) attr._IsPrimary = trycol.IsPrimary; - if (trycol._IsIdentity != null) attr._IsIdentity = trycol.IsIdentity; - if (trycol._IsNullable != null) attr._IsNullable = trycol.IsNullable; - if (trycol._IsIgnore != null) attr._IsIgnore = trycol.IsIgnore; - if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion; - if (trycol.MapType != null) attr.MapType = trycol.MapType; - if (trycol._Position != null) attr._Position = trycol.Position; - if (trycol._CanInsert != null) attr._CanInsert = trycol.CanInsert; - if (trycol._CanUpdate != null) attr._CanUpdate = trycol.CanUpdate; - if (trycol.ServerTime != DateTimeKind.Unspecified) attr.ServerTime = trycol.ServerTime; - if (trycol._StringLength != null) attr.StringLength = trycol.StringLength; - if (!string.IsNullOrEmpty(trycol.InsertValueSql)) attr.InsertValueSql = trycol.InsertValueSql; - if (trycol._Precision != null) attr.Precision = trycol.Precision; - if (trycol._Scale != null) attr.Scale = trycol.Scale; - if (!string.IsNullOrEmpty(trycol.RewriteSql)) attr.RewriteSql = trycol.RewriteSql; - if (!string.IsNullOrEmpty(trycol.RereadSql)) attr.RereadSql = trycol.RereadSql; - } - var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false); - foreach (var tryattrobj in attrs) - { - var tryattr = tryattrobj as ColumnAttribute; - if (tryattr == null) continue; - 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; + switch (mp) + { + case MappingPriorityType.Aop: + if (_orm.Aop.ConfigEntityPropertyHandler != null) + { + var aope = new Aop.ConfigEntityPropertyEventArgs(type, proto); + _orm.Aop.ConfigEntityPropertyHandler(_orm, aope); + 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; + } + break; + case MappingPriorityType.FluentApi: + 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.OldName)) attr.OldName = trycol.OldName; + if (!string.IsNullOrEmpty(trycol.DbType)) attr.DbType = trycol.DbType; + if (trycol._IsPrimary != null) attr._IsPrimary = trycol.IsPrimary; + if (trycol._IsIdentity != null) attr._IsIdentity = trycol.IsIdentity; + if (trycol._IsNullable != null) attr._IsNullable = trycol.IsNullable; + if (trycol._IsIgnore != null) attr._IsIgnore = trycol.IsIgnore; + if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion; + if (trycol.MapType != null) attr.MapType = trycol.MapType; + if (trycol._Position != null) attr._Position = trycol.Position; + if (trycol._CanInsert != null) attr._CanInsert = trycol.CanInsert; + if (trycol._CanUpdate != null) attr._CanUpdate = trycol.CanUpdate; + if (trycol.ServerTime != DateTimeKind.Unspecified) attr.ServerTime = trycol.ServerTime; + if (trycol._StringLength != null) attr.StringLength = trycol.StringLength; + if (!string.IsNullOrEmpty(trycol.InsertValueSql)) attr.InsertValueSql = trycol.InsertValueSql; + if (trycol._Precision != null) attr.Precision = trycol.Precision; + if (trycol._Scale != null) attr.Scale = trycol.Scale; + if (!string.IsNullOrEmpty(trycol.RewriteSql)) attr.RewriteSql = trycol.RewriteSql; + if (!string.IsNullOrEmpty(trycol.RereadSql)) attr.RereadSql = trycol.RereadSql; + } + break; + case MappingPriorityType.Attribute: + var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false); + foreach (var tryattrobj in attrs) + { + var tryattr = tryattrobj as ColumnAttribute; + if (tryattr == null) continue; + 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; + } + break; + } } ColumnAttribute ret = null; if (!string.IsNullOrEmpty(attr.Name)) ret = attr; @@ -228,18 +274,31 @@ namespace FreeSql.Internal public NavigateAttribute GetEntityNavigateAttribute(Type type, PropertyInfo proto) { var attr = new NavigateAttribute(); - if (dicConfigEntity.TryGetValue(type, out var trytb) && trytb._navigates.TryGetValue(proto.Name, out var trynav)) + foreach (var mp in _mappingPriorityTypes) { - if (!string.IsNullOrEmpty(trynav.Bind)) attr.Bind = trynav.Bind; - if (trynav.ManyToMany != null) attr.ManyToMany = trynav.ManyToMany; - } - var attrs = proto.GetCustomAttributes(typeof(NavigateAttribute), false); - foreach (var tryattrobj in attrs) - { - trynav = tryattrobj as NavigateAttribute; - if (trynav == null) continue; - if (!string.IsNullOrEmpty(trynav.Bind)) attr.Bind = trynav.Bind; - if (trynav.ManyToMany != null) attr.ManyToMany = trynav.ManyToMany; + 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 (!string.IsNullOrEmpty(trynav.Bind)) attr.Bind = trynav.Bind; + if (trynav.ManyToMany != null) attr.ManyToMany = trynav.ManyToMany; + } + break; + case MappingPriorityType.Attribute: + var attrs = proto.GetCustomAttributes(typeof(NavigateAttribute), false); + foreach (var tryattrobj in attrs) + { + trynav = tryattrobj as NavigateAttribute; + if (trynav == null) continue; + if (!string.IsNullOrEmpty(trynav.Bind)) attr.Bind = trynav.Bind; + if (trynav.ManyToMany != null) attr.ManyToMany = trynav.ManyToMany; + } + break; + } } NavigateAttribute ret = null; if (!string.IsNullOrEmpty(attr.Bind)) ret = attr; @@ -249,35 +308,47 @@ namespace FreeSql.Internal public IndexAttribute[] GetEntityIndexAttribute(Type type) { var ret = new Dictionary(); - if (_orm.Aop.ConfigEntityHandler != null) + foreach (var mp in _mappingPriorityTypes) { - var aope = new Aop.ConfigEntityEventArgs(type); - _orm.Aop.ConfigEntityHandler(_orm, aope); - foreach (var idxattr in aope.ModifyIndexResult) - if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) - { - if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); - ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); - } - } - if (dicConfigEntity.TryGetValue(type, out var trytb)) - { - foreach (var idxattr in trytb._indexs.Values) - if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) - { - if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); - ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); - } - } - var attrs = type.GetCustomAttributes(typeof(IndexAttribute), true); - foreach (var tryattrobj in attrs) - { - var idxattr = tryattrobj as IndexAttribute; - if (idxattr == null) continue; - if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) + switch (mp) { - if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); - ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + case MappingPriorityType.Aop: + if (_orm.Aop.ConfigEntityHandler != null) + { + var aope = new Aop.ConfigEntityEventArgs(type); + _orm.Aop.ConfigEntityHandler(_orm, aope); + foreach (var idxattr in aope.ModifyIndexResult) + if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) + { + if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); + ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + } + } + break; + case MappingPriorityType.FluentApi: + if (dicConfigEntity.TryGetValue(type, out var trytb)) + { + foreach (var idxattr in trytb._indexs.Values) + if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) + { + if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); + ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + } + } + break; + case MappingPriorityType.Attribute: + var attrs = type.GetCustomAttributes(typeof(IndexAttribute), true); + foreach (var tryattrobj in attrs) + { + var idxattr = tryattrobj as IndexAttribute; + if (idxattr == null) continue; + if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields)) + { + if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name); + ret.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique }); + } + } + break; } } return ret.Values.ToArray(); diff --git a/FreeSql/Internal/StringConvertType.cs b/FreeSql/Internal/FreeSqlBuilderTypes.cs similarity index 73% rename from FreeSql/Internal/StringConvertType.cs rename to FreeSql/Internal/FreeSqlBuilderTypes.cs index da7e6104..4d3a4541 100644 --- a/FreeSql/Internal/StringConvertType.cs +++ b/FreeSql/Internal/FreeSqlBuilderTypes.cs @@ -2,47 +2,31 @@ namespace FreeSql.Internal { - public enum StringConvertType + /// + /// 映射优先级,默认: Attribute > FluentApi > Aop + /// + public enum MappingPriorityType { /// - /// 不进行任何处理 + /// 实体特性 + /// [Table(Name = "tabname")] + /// [Column(Name = "table_id")] /// - None = 0, + Attribute = 0, /// - /// 将帕斯卡命名字符串转换为下划线分隔字符串 - /// - /// BigApple -> Big_Apple + /// 流式接口 + /// fsql.CodeFirst.ConfigEntity(a => a.Name("tabname")) + /// fsql.CodeFirst.ConfigEntity(a => a.Property(b => b.Id).Name("table_id")) /// - PascalCaseToUnderscore, + FluentApi, /// - /// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写 - /// - /// BigApple -> BIG_APPLE + /// AOP 特性 https://github.com/dotnetcore/FreeSql/wiki/AOP + /// fsql.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = "public.tabname"; + /// fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = "table_id"; /// - PascalCaseToUnderscoreWithUpper, - - /// - /// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写 - /// - /// BigApple -> big_apple - /// - PascalCaseToUnderscoreWithLower, - - /// - /// 将字符串转换为大写 - /// - /// BigApple -> BIGAPPLE - /// - Upper, - - /// - /// 将字符串转换为小写 - /// - /// BigApple -> bigapple - /// - Lower + Aop } public enum NameConvertType @@ -94,4 +78,48 @@ namespace FreeSql.Internal /// ToLower } + + public enum StringConvertType + { + /// + /// 不进行任何处理 + /// + None = 0, + + /// + /// 将帕斯卡命名字符串转换为下划线分隔字符串 + /// + /// BigApple -> Big_Apple + /// + PascalCaseToUnderscore, + + /// + /// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写 + /// + /// BigApple -> BIG_APPLE + /// + PascalCaseToUnderscoreWithUpper, + + /// + /// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写 + /// + /// BigApple -> big_apple + /// + PascalCaseToUnderscoreWithLower, + + /// + /// 将字符串转换为大写 + /// + /// BigApple -> BIGAPPLE + /// + Upper, + + /// + /// 将字符串转换为小写 + /// + /// BigApple -> bigapple + /// + Lower + } + } \ No newline at end of file