Skip to content

Commit

Permalink
Code First Change Data Type synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
DotNetNext committed Jun 21, 2017
1 parent bc99b5f commit 47980cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions SqlServerTest/Demos/5_CodeFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class CodeTable
public int Id { get; set; }
[SugarColumn(Length = 21,OldColumnName = "Name2")]
public string Name{ get; set; }
[SugarColumn(IsNullable = true)]
public bool IsOk { get; set; }
[SugarColumn(IsNullable = true,Length =10)]
public string IsOk { get; set; }
public Guid Guid { get; set; }
public decimal Decimal { get; set; }
[SugarColumn(IsNullable = true)]
Expand Down
11 changes: 10 additions & 1 deletion SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ private void ExistLogic(EntityInfo entityInfo)
.Where(ec => !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
.Where(ec =>
dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName)
&& ((ec.Length != dc.Length && PubMethod.GetUnderType(ec.PropertyInfo).IsIn(PubConst.StringType)) || ec.IsNullable != dc.IsNullable))).ToList();
&& ((ec.Length != dc.Length && PubMethod.GetUnderType(ec.PropertyInfo).IsIn(PubConst.StringType)) ||
ec.IsNullable != dc.IsNullable ||
IsSamgeType(ec, dc)))).ToList();
var renameColumns = entityColumns
.Where(it => !string.IsNullOrEmpty(it.OldDbColumnName))
.Where(entityColumn => dbColumns.Any(dbColumn => entityColumn.OldDbColumnName.Equals(dbColumn.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))
Expand Down Expand Up @@ -211,6 +213,13 @@ private DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableN
};
return result;
}

private bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc)
{
var propType = this.Context.Ado.DbBind.GetDbTypeName(PubMethod.GetUnderType(ec.PropertyInfo).Name);
var dataType = dc.DataType;
return propType != dataType;
}
#endregion
}
}

0 comments on commit 47980cd

Please sign in to comment.