Browse Source

Add CodeFirst.SetStringDefaultLength

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
221eac6dcb
  1. 3
      Src/Asp.Net/SqlServerTest/Demos/5_CodeFirst.cs
  2. 15
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  3. 1
      Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs

3
Src/Asp.Net/SqlServerTest/Demos/5_CodeFirst.cs

@ -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));
}
}
}

15
Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs

@ -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)

1
Src/Asp.Net/SqlSugar/Interface/ICodeFirst.cs

@ -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);

Loading…
Cancel
Save