mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
CodeFirst support not null default
This commit is contained in:
parent
4186e0820e
commit
5bdf1ca4a7
@ -15,8 +15,31 @@ namespace OrmTest
|
|||||||
Db.DbMaintenance.DropTable("UnitCodeTest1");
|
Db.DbMaintenance.DropTable("UnitCodeTest1");
|
||||||
Db.CodeFirst.InitTables<UnitCodeTest1>();
|
Db.CodeFirst.InitTables<UnitCodeTest1>();
|
||||||
Db.CodeFirst.InitTables<UnitCodeFirstpks2>();
|
Db.CodeFirst.InitTables<UnitCodeFirstpks2>();
|
||||||
|
if (Db.DbMaintenance.IsAnyTable("UnitCodeFirst131", false))
|
||||||
|
Db.DbMaintenance.DropTable("UnitCodeFirst131");
|
||||||
|
Db.CodeFirst.InitTables<UnitCodeFirst131>();
|
||||||
|
Db.Insertable(new UnitCodeFirst131() { Id = 1 }).ExecuteCommand();
|
||||||
|
Db.CodeFirst.InitTables<UNITCODEFIRST131>();
|
||||||
|
Db.CodeFirst.InitTables<UNITCOdEFIRST131>();
|
||||||
|
}
|
||||||
|
public class UnitCodeFirst131
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
public class UNITCODEFIRST131
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
[SqlSugar.SugarColumn(DefaultValue ="a")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
public class UNITCOdEFIRST131
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
[SqlSugar.SugarColumn(DefaultValue = "a")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
[SqlSugar.SugarColumn(DefaultValue = "getdate()")]
|
||||||
|
public DateTime dt { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UnitCodeFirstpks2
|
public class UnitCodeFirstpks2
|
||||||
{
|
{
|
||||||
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
|
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
|
||||||
|
@ -196,8 +196,27 @@ namespace SqlSugar
|
|||||||
public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo)
|
public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo)
|
||||||
{
|
{
|
||||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
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);
|
string sql = GetAddColumnSql(tableName, columnInfo);
|
||||||
this.Context.Ado.ExecuteCommand(sql);
|
this.Context.Ado.ExecuteCommand(sql);
|
||||||
|
if (isAddNotNUll)
|
||||||
|
{
|
||||||
|
var dtColums = this.Context.Queryable<object>().AS(columnInfo.TableName).Where("1=2").ToDataTable().Columns.Cast<System.Data.DataColumn>();
|
||||||
|
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<string, object>();
|
||||||
|
dt.Add(columnInfo.DbColumnName, value);
|
||||||
|
this.Context.Updateable(dt)
|
||||||
|
.AS(tableName)
|
||||||
|
.Where($"{columnInfo.DbColumnName} is null ").ExecuteCommand();
|
||||||
|
UpdateColumn(tableName, columnInfo);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public virtual bool UpdateColumn(string tableName, DbColumnInfo column)
|
public virtual bool UpdateColumn(string tableName, DbColumnInfo column)
|
||||||
|
Loading…
Reference in New Issue
Block a user