From 96d7efdfabd640cd2c352a4a38ae6e1f4d94fd41 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 26 Aug 2017 01:57:24 +0800 Subject: [PATCH] - --- .../SqlServerTest/Demos/5_CodeFirst.cs | 4 ++- .../Abstract/DbMaintenanceProvider/Methods.cs | 30 ++++++++++++++----- Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs | 1 + .../Entities/Mapping/SugarMappingAttribute.cs | 4 +-- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demos/5_CodeFirst.cs b/Src/Asp.Net/SqlServerTest/Demos/5_CodeFirst.cs index 165dfd999..965991457 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/5_CodeFirst.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/5_CodeFirst.cs @@ -22,8 +22,10 @@ namespace OrmTest.Demo public DateTime? DateTime { get; set; } [SugarColumn(IsNullable = true,OldColumnName = "Dob")] public double? Dob2 { get; set; } - [SugarColumn(Length =110)] + [SugarColumn(Length =11000)] public string A1 { get; set; } + [SugarColumn(Length = 18,DecimalDigits=2)] + public decimal Dec { get; set; } } public class CodeTable2 { public int Id { get; set; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index 43d4fa465..60081ec3d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -238,10 +238,7 @@ namespace SqlSugar { string columnName = this.SqlBuilder.GetTranslationTableName(item.DbColumnName); string dataType = item.DataType; - string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null; - if (item.Length>4000|| item.Length==-1) { - dataSize = string.Format("({0})","max"); - } + string dataSize = GetSize(item); string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull; string primaryKey = null; string identity = item.IsIdentity ? this.CreateTableIdentity : null; @@ -251,12 +248,31 @@ namespace SqlSugar string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray)); return tableString; } + + protected virtual string GetSize(DbColumnInfo item) + { + string dataSize = null; + var isMax = item.Length > 4000 || item.Length == -1; + if (isMax) { + dataSize = item.Length > 0 ? string.Format("({0})", "max") : null; + } + else if (item.Length > 0 && item.DecimalDigits == 0) + { + dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null; + } + else if (item.Length > 0 && item.DecimalDigits > 0) + { + dataSize = item.Length > 0 ? string.Format("({0},{1})", item.Length, item.DecimalDigits) : null; + } + return dataSize; + } + protected virtual string GetAddColumnSql(string tableName, DbColumnInfo columnInfo) { string columnName = this.SqlBuilder.GetTranslationColumnName(columnInfo.DbColumnName); tableName = this.SqlBuilder.GetTranslationTableName(tableName); string dataType = columnInfo.DataType; - string dataSize = columnInfo.Length > 0 ? string.Format("({0})", columnInfo.Length) : null; + string dataSize = GetSize(columnInfo); string nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull; string primaryKey = null; string identity = null; @@ -268,7 +284,7 @@ namespace SqlSugar string columnName = this.SqlBuilder.GetTranslationTableName(columnInfo.DbColumnName); tableName = this.SqlBuilder.GetTranslationTableName(tableName); string dataType = columnInfo.DataType; - string dataSize = columnInfo.Length > 0 ? string.Format("({0})", columnInfo.Length) : null; + string dataSize = GetSize(columnInfo); string nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull; string primaryKey = null; string identity = null; @@ -277,7 +293,7 @@ namespace SqlSugar } protected virtual string GetCacheKey(string cacheKey) { - return this.Context.CurrentConnectionConfig.DbType + "." + this.Context.Ado.Connection.Database +"."+ cacheKey; + return this.Context.CurrentConnectionConfig.DbType + "." + this.Context.Ado.Connection.Database + "." + cacheKey; } #endregion } diff --git a/Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs b/Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs index e9f81858a..19d382958 100644 --- a/Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs +++ b/Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs @@ -19,5 +19,6 @@ namespace SqlSugar public bool IsIdentity { get; set; } public bool IsPrimarykey { get; set; } public object Value { get; set; } + public int DecimalDigits { get; set; } } } diff --git a/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs b/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs index 9af6b191e..e7e9ac375 100644 --- a/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs +++ b/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs @@ -87,8 +87,8 @@ namespace SqlSugar set { _ColumnDataType = value; } } - private int? _DecimalDigits; - private int? DecimalDigits { + private int _DecimalDigits; + public int DecimalDigits { get { return _DecimalDigits; } set { _DecimalDigits = value; } }