diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs index 31090b30e..a75a61750 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbFirstProvider/DbFirstProvider.cs @@ -206,7 +206,45 @@ namespace SqlSugar classText = classText.Replace(DbFirstTemplate.KeyPropertyName, null); return classText; } - + internal string GetClassString(List columns, ref string className) + { + string classText = this.ClassTemplate; + string ConstructorText = IsDefaultValue ? this.ConstructorTemplate : null; + 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); + classText = classText.Replace(DbFirstTemplate.KeyClassDescription, this.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription,"\r\n")); + classText = classText.Replace(DbFirstTemplate.KeySugarTable, IsAttribute ? string.Format(DbFirstTemplate.ValueSugarTable, className) : null); + if (columns.IsValuable()) + { + foreach (var item in columns) + { + var isLast = columns.Last() == item; + var index = columns.IndexOf(item); + string PropertyText = this.PropertyTemplate; + string PropertyDescriptionText = this.PropertyDescriptionTemplate; + string propertyName = GetPropertyName(item); + string propertyTypeName =item.DataType; + PropertyText = GetPropertyText(item, PropertyText); + PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText); + PropertyText = PropertyDescriptionText + PropertyText; + classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast ? "" : ("\r\n" + DbFirstTemplate.KeyPropertyName))); + if (ConstructorText.IsValuable() && item.DefaultValue != null) + { + var hasDefaultValue = columns.Skip(index + 1).Any(it => it.DefaultValue.IsValuable()); + ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyName, propertyName); + ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) + (!hasDefaultValue ? "" : this.ConstructorTemplate); + } + } + } + if (!columns.Any(it => it.DefaultValue != null)) + { + ConstructorText = null; + } + classText = classText.Replace(DbFirstTemplate.KeyConstructor, ConstructorText); + classText = classText.Replace(DbFirstTemplate.KeyPropertyName, null); + return classText; + } public void CreateClassFile(string directoryPath, string nameSpace = "Models") { Check.ArgumentNullException(directoryPath, "directoryPath can't null"); diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 46ea6e778..f5c5f9460 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -605,6 +605,21 @@ namespace SqlSugar } return this; } + public string ToClassString(string className) + { + List columns = new List(); + var properties = typeof(T).GetProperties(); + foreach (var item in properties) + { + columns.Add(new DbColumnInfo() + { + DbColumnName=item.Name, + DataType=UtilMethods.GetUnderType(item.PropertyType).Name + }); + } + var result = ((this.Context.DbFirst) as DbFirstProvider).GetClassString(columns, ref className); + return result; + } #region Async methods public Task SingleAsync() { @@ -1009,7 +1024,7 @@ namespace SqlSugar if (IsCache) { var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCache; - result = CacheSchemeMain.GetOrCreate>(cacheService, this.QueryBuilder,()=> { return GetData(sqlObj); },CacheTime, this.Context); + result = CacheSchemeMain.GetOrCreate>(cacheService, this.QueryBuilder, () => { return GetData(sqlObj); }, CacheTime, this.Context); } else { diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index 16e8797f0..d24537fe1 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -123,6 +123,7 @@ namespace SqlSugar Task,int>> ToPageListAsync(int pageIndex, int pageSize, int totalNumber); ISugarQueryable WithCache(int cacheDurationInSeconds = int.MaxValue); ISugarQueryable WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue); + string ToClassString(); void Clear(); } public partial interface ISugarQueryable : ISugarQueryable