mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 01:58:13 +08:00
Add CodeFirst.SetStringDefaultLength
This commit is contained in:
@@ -15,7 +15,6 @@ namespace OrmTest.Demo
|
||||
public int Id { get; set; }
|
||||
[SugarColumn(Length = 21,OldColumnName = "Name2")]
|
||||
public string Name{ get; set; }
|
||||
[SugarColumn(IsNullable = true,Length =10)]
|
||||
public string IsOk { get; set; }
|
||||
public Guid Guid { get; set; }
|
||||
[SugarColumn(ColumnDataType ="int")]
|
||||
@@ -51,7 +50,7 @@ namespace OrmTest.Demo
|
||||
//db.CodeFirst.BackupTable().InitTables(typeof(CodeTable),typeof(CodeTable2));
|
||||
|
||||
//No backup table
|
||||
db.CodeFirst.InitTables(typeof(CodeTable),typeof(CodeTable2));
|
||||
db.CodeFirst.SetStringDefaultLength(10).InitTables(typeof(CodeTable),typeof(CodeTable2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ namespace SqlSugar
|
||||
public virtual SqlSugarClient Context { get; set; }
|
||||
private bool IsBackupTable { get; set; }
|
||||
private int MaxBackupDataRows { get; set; }
|
||||
private int DefultLength { get; set; }
|
||||
#endregion
|
||||
|
||||
#region Public methods
|
||||
@@ -20,6 +21,12 @@ namespace SqlSugar
|
||||
this.MaxBackupDataRows = maxBackupDataRows;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual ICodeFirst SetStringDefaultLength(int length) {
|
||||
DefultLength = length;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual void InitTables(Type entityType)
|
||||
{
|
||||
|
||||
@@ -67,6 +74,14 @@ namespace SqlSugar
|
||||
protected virtual void Execute(Type entityType)
|
||||
{
|
||||
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(entityType);
|
||||
if (this.DefultLength > 0) {
|
||||
foreach (var item in entityInfo.Columns)
|
||||
{
|
||||
if (item.PropertyInfo.PropertyType == UtilConstants.StringType && item.DataType.IsNullOrEmpty()&& item.Length==0) {
|
||||
item.Length = DefultLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
var tableName = GetTableName(entityInfo);
|
||||
var isAny = this.Context.DbMaintenance.IsAnyTable(tableName);
|
||||
if (isAny)
|
||||
|
@@ -8,6 +8,7 @@ namespace SqlSugar
|
||||
{
|
||||
SqlSugarClient Context { get; set; }
|
||||
ICodeFirst BackupTable(int maxBackupDataRows=int.MaxValue);
|
||||
ICodeFirst SetStringDefaultLength(int length);
|
||||
void InitTables(string entitiesNamespace);
|
||||
void InitTables(string [] entitiesNamespaces);
|
||||
void InitTables(params Type [] entityTypes);
|
||||
|
Reference in New Issue
Block a user