Skip to content

Commit 7290109

Browse files
committed
- 增加 UseMappingPriority 指定映射优先级;#387 #69 #99
1 parent 184d11d commit 7290109

File tree

4 files changed

+380
-208
lines changed

4 files changed

+380
-208
lines changed

FreeSql/FreeSql.xml

Lines changed: 125 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/FreeSqlBuilder.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Reflection;
77
using FreeSql.DataAnnotations;
88
using FreeSql.Internal;
9+
using FreeSql.Internal.CommonProvider;
910

1011
namespace FreeSql
1112
{
@@ -24,6 +25,7 @@ public partial class FreeSqlBuilder
2425
bool _isGenerateCommandParameterWithLambda = false;
2526
bool _isLazyLoading = false;
2627
bool _isExitAutoDisposePool = true;
28+
MappingPriorityType[] _mappingPriorityTypes;
2729
StringConvertType _entityPropertyConvertType = StringConvertType.None;
2830
NameConvertType _nameConvertType = NameConvertType.None;
2931
Action<DbCommand> _aopCommandExecuting = null;
@@ -160,6 +162,29 @@ public FreeSqlBuilder UseNameConvert(NameConvertType convertType)
160162
return this;
161163
}
162164

165+
/// <summary>
166+
/// 指定映射优先级<para></para>
167+
/// 例如表名:实体类名 &lt; Aop &lt; FluentApi &lt; Attribute &lt; AsTable<para></para>
168+
/// 事件 Aop -------> fsql.Aop.ConfigEntity/fsql.Aop.ConfigEntityProperty<para></para>
169+
/// 方法 FluentApi -> fsql.CodeFirst.ConfigEntity/fsql.CodeFirst.Entity<para></para>
170+
/// 特性 Attribute -> [Table(Name = xxx, ...)]<para></para>
171+
/// -----------------------------------------------------------------------------<para></para>
172+
/// 默认规则:关于映射优先级,Attribute 可以更直观排查问题,即使任何地方使用 FluentApi/Aop 设置 TableName 都不生效。<para></para>
173+
/// 调整规则:UseMappingPriority(Attribute, FluentApi, Aop) <para></para>
174+
/// 实体类名 &lt; Attribute &lt; FluentApi &lt; Aop &lt; AsTable
175+
/// </summary>
176+
/// <param name="mappingType1"></param>
177+
/// <param name="mappingType2"></param>
178+
/// <param name="mappingType3"></param>
179+
/// <returns></returns>
180+
/// <exception cref="ArgumentException"></exception>
181+
public FreeSqlBuilder UseMappingPriority(MappingPriorityType mappingType1, MappingPriorityType mappingType2, MappingPriorityType mappingType3)
182+
{
183+
if (mappingType1 == mappingType2 || mappingType1 == mappingType3 || mappingType2 == mappingType3) throw new ArgumentException($"{nameof(mappingType1)}{nameof(mappingType2)}{nameof(mappingType3)} 不可以相等");
184+
_mappingPriorityTypes = new[] { mappingType1, mappingType2, mappingType3 };
185+
return this;
186+
}
187+
163188
/// <summary>
164189
/// 监听 AppDomain.CurrentDomain.ProcessExit/Console.CancelKeyPress 事件自动释放连接池<para></para>
165190
/// 默认值: true
@@ -298,6 +323,9 @@ public IFreeSql<TMark> Build<TMark>()
298323
ret.CodeFirst.IsGenerateCommandParameterWithLambda = _isGenerateCommandParameterWithLambda;
299324
ret.CodeFirst.IsLazyLoading = _isLazyLoading;
300325

326+
if (_mappingPriorityTypes != null)
327+
(ret.Select<object>() as Select0Provider)._commonUtils._mappingPriorityTypes = _mappingPriorityTypes;
328+
301329
if (_aopCommandExecuting != null)
302330
ret.Aop.CommandBefore += new EventHandler<Aop.CommandBeforeEventArgs>((s, e) => _aopCommandExecuting?.Invoke(e.Command));
303331
if (_aopCommandExecuted != null)

0 commit comments

Comments
 (0)