11using System ;
22using System . Data . Common ;
3+ using System . Linq ;
4+ using System . Reflection ;
5+ using FreeSql . DataAnnotations ;
6+ using FreeSql . Internal ;
37
48namespace FreeSql {
59 public class FreeSqlBuilder {
@@ -12,6 +16,7 @@ public class FreeSqlBuilder {
1216 bool _isConfigEntityFromDbFirst = false ;
1317 bool _isNoneCommandParameter = false ;
1418 bool _isLazyLoading = false ;
19+ StringConvertType _entityPropertyConvertType = StringConvertType . None ;
1520 Action < DbCommand > _aopCommandExecuting = null ;
1621 Action < DbCommand , string > _aopCommandExecuted = null ;
1722
@@ -101,7 +106,20 @@ public FreeSqlBuilder UseMonitorCommand(Action<DbCommand> executing, Action<DbCo
101106 return this ;
102107 }
103108
104- public IFreeSql Build ( ) => Build < IFreeSql > ( ) ;
109+ /// <summary>
110+ /// 自动转换实体属性名称 Entity Property -> Db Filed
111+ /// <para></para>
112+ /// *不会覆盖 [Column] 特性设置的Name
113+ /// </summary>
114+ /// <param name="convertType"></param>
115+ /// <returns></returns>
116+ public FreeSqlBuilder UseEntityPropertyNameConvert ( StringConvertType convertType )
117+ {
118+ _entityPropertyConvertType = convertType ;
119+ return this ;
120+ }
121+
122+ public IFreeSql Build ( ) => Build < IFreeSql > ( ) ;
105123 public IFreeSql < TMark > Build < TMark > ( ) {
106124 if ( string . IsNullOrEmpty ( _masterConnectionString ) ) throw new Exception ( "参数 masterConnectionString 不可为空,请检查 UseConnectionString" ) ;
107125 IFreeSql < TMark > ret = null ;
@@ -138,7 +156,75 @@ public IFreeSql<TMark> Build<TMark>() {
138156 var ado = ret . Ado as Internal . CommonProvider . AdoProvider ;
139157 ado . AopCommandExecuting += _aopCommandExecuting ;
140158 ado . AopCommandExecuted += _aopCommandExecuted ;
141- }
159+
160+ //添加实体属性名全局AOP转换处理
161+ if ( _entityPropertyConvertType != StringConvertType . None )
162+ {
163+ // 局部方法判断是否存在Column特性以及是否设置Name的值
164+ bool CheckEntityPropertyColumnAttribute ( PropertyInfo propertyInfo )
165+ {
166+ var attr = propertyInfo . GetCustomAttribute < ColumnAttribute > ( ) ;
167+ if ( attr == null || string . IsNullOrEmpty ( attr . Name ) )
168+ {
169+ return true ;
170+ }
171+
172+ return false ;
173+ }
174+
175+ switch ( _entityPropertyConvertType )
176+ {
177+ case StringConvertType . Lower :
178+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
179+ {
180+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
181+ {
182+ e . ModifyResult . Name = e . Property . Name . ToLower ( ) ;
183+ }
184+ } ;
185+ break ;
186+ case StringConvertType . Upper :
187+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
188+ {
189+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
190+ {
191+ e . ModifyResult . Name = e . Property . Name . ToUpper ( ) ;
192+ }
193+ } ;
194+ break ;
195+ case StringConvertType . PascalCaseToUnderscore :
196+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
197+ {
198+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
199+ {
200+ e . ModifyResult . Name = StringUtils . PascalCaseToUnderScore ( e . Property . Name ) ;
201+ }
202+ } ;
203+ break ;
204+ case StringConvertType . PascalCaseToUnderscoreWithLower :
205+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
206+ {
207+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
208+ {
209+ e . ModifyResult . Name = StringUtils . PascalCaseToUnderScore ( e . Property . Name ) . ToLower ( ) ;
210+ }
211+ } ;
212+ break ;
213+ case StringConvertType . PascalCaseToUnderscoreWithUpper :
214+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
215+ {
216+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
217+ {
218+ e . ModifyResult . Name = StringUtils . PascalCaseToUnderScore ( e . Property . Name ) . ToUpper ( ) ;
219+ }
220+ } ;
221+ break ;
222+ default :
223+ break ;
224+ }
225+ }
226+ }
227+
142228 return ret ;
143229 }
144230 }
0 commit comments