diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index ddf896151..73b02d198 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -196,8 +196,27 @@ namespace SqlSugar public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo) { tableName = this.SqlBuilder.GetTranslationTableName(tableName); + var isAddNotNUll = columnInfo.IsNullable == false && columnInfo.DefaultValue.HasValue(); + if (isAddNotNUll) + { + columnInfo = this.Context.Utilities.TranslateCopy(columnInfo); + columnInfo.IsNullable = true; + } string sql = GetAddColumnSql(tableName, columnInfo); this.Context.Ado.ExecuteCommand(sql); + if (isAddNotNUll) + { + var dtColums = this.Context.Queryable().AS(columnInfo.TableName).Where("1=2").ToDataTable().Columns.Cast(); + var dtColumInfo = dtColums.First(it => it.ColumnName.EqualCase(columnInfo.DbColumnName)); + var type = UtilMethods.GetUnderType(dtColumInfo.DataType); + var value= type==UtilConstants.StringType?(object)"": Activator.CreateInstance(type); + var dt = new Dictionary(); + dt.Add(columnInfo.DbColumnName, value); + this.Context.Updateable(dt) + .AS(tableName) + .Where($"{columnInfo.DbColumnName} is null ").ExecuteCommand(); + UpdateColumn(tableName, columnInfo); + } return true; } public virtual bool UpdateColumn(string tableName, DbColumnInfo column)