diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs index dd2a841cc..654af8e94 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs @@ -21,6 +21,8 @@ namespace SqlSugar private bool IsDefaultValue { get; set; } private Func WhereColumnsfunc; private Func FormatFileNameFunc { get; set; } + private Func FormatClassNameFunc { get; set; } + private Func FormatPropertyNameFunc { get; set; } private bool IsStringNullable {get;set; } private Func PropertyTextTemplateFunc { get; set; } private Func ReplaceClassStringFunc { get; set; } @@ -167,6 +169,16 @@ namespace SqlSugar this.FormatFileNameFunc = formatFileNameFunc; return this; } + public IDbFirst FormatClassName(Func formatClassNameFunc) + { + this.FormatClassNameFunc = formatClassNameFunc; + return this; + } + public IDbFirst FormatPropertyName(Func formatPropertyNameFunc) + { + this.FormatPropertyNameFunc = formatPropertyNameFunc; + return this; + } public IDbFirst CreatedReplaceClassString(Func replaceClassStringFunc) { this.ReplaceClassStringFunc = replaceClassStringFunc; @@ -223,12 +235,17 @@ namespace SqlSugar { string classText = null; string className = tableInfo.Name; + var oldClasName = className; classText = GetClassString(tableInfo, ref className); result.Remove(className); if (this.ReplaceClassStringFunc != null) { classText=this.ReplaceClassStringFunc(classText); } + if (FormatClassNameFunc != null&&FormatFileNameFunc != null) + { + className = oldClasName; + } result.Add(className, classText); } catch (Exception ex) @@ -265,6 +282,10 @@ namespace SqlSugar classText = classText.Replace(DbFirstTemplate.KeyClassName, mappingInfo.EntityName); } } + if (FormatClassNameFunc != null) + { + className=FormatClassNameFunc(className); + } classText = classText.Replace(DbFirstTemplate.KeyClassName, className); classText = classText.Replace(DbFirstTemplate.KeyNamespace, this.Namespace); classText = classText.Replace(DbFirstTemplate.KeyUsing, IsAttribute ? (this.UsingTemplate + "using " + UtilConstants.AssemblyName + ";\r\n") : this.UsingTemplate); @@ -278,7 +299,12 @@ namespace SqlSugar var index = columns.IndexOf(item); string PropertyText = this.PropertyTemplate; string PropertyDescriptionText = this.PropertyDescriptionTemplate; - string propertyName = GetPropertyName(item); + string propertyName = GetPropertyName(item); + var oldPropertyName = propertyName; + if (FormatPropertyNameFunc != null) + { + item.DbColumnName=propertyName = FormatPropertyNameFunc(propertyName); + } string propertyTypeName = GetPropertyTypeName(item); PropertyText =this.PropertyTextTemplateFunc == null? GetPropertyText(item, PropertyText):this.PropertyTextTemplateFunc(item,this.PropertyTemplate, propertyTypeName); PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText); @@ -286,10 +312,21 @@ namespace SqlSugar { PropertyDescriptionText += "\r\n [SugarColumn(IsArray=true)]"; } - else if (item?.DataType?.StartsWith("json")==true) + else if (item?.DataType?.StartsWith("json") == true) { PropertyDescriptionText += "\r\n [SugarColumn(IsJson=true)]"; } + else if (FormatPropertyNameFunc != null) + { + if (PropertyText.Contains("SugarColumn")) + { + PropertyText = PropertyText.Replace(")]", ",ColumnName=\"" + oldPropertyName + "\")]"); + } + else + { + PropertyDescriptionText += "\r\n [SugarColumn(ColumnName=\"" + oldPropertyName + "\")]"; + } + } PropertyText = PropertyDescriptionText + PropertyText; classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast ? "" : ("\r\n" + DbFirstTemplate.KeyPropertyName))); if (ConstructorText.HasValue() && item.DefaultValue != null) diff --git a/Src/Asp.Net/SqlSugar/Interface/IDbFirst.cs b/Src/Asp.Net/SqlSugar/Interface/IDbFirst.cs index e1caa9c35..55bb4d4f9 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IDbFirst.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IDbFirst.cs @@ -25,6 +25,8 @@ namespace SqlSugar Dictionary ToClassStringList(string nameSpace = "Models"); void Init(); IDbFirst FormatFileName(Func formatFileNameFunc); + IDbFirst FormatClassName(Func formatClassNameFunc); + IDbFirst FormatPropertyName(Func formatPropertyNameFunc); IDbFirst StringNullable(); IDbFirst CreatedReplaceClassString(Func replaceClassStringFunc); }