Update CodeFirst

This commit is contained in:
sunkaixuan
2017-06-25 19:16:10 +08:00
parent 775977fcf8
commit 2a30d4f62c
6 changed files with 57 additions and 23 deletions

View File

@@ -21,8 +21,8 @@ namespace OrmTest.Demo
public DateTime? DateTime { get; set; } public DateTime? DateTime { get; set; }
[SugarColumn(IsNullable = true,OldColumnName = "Dob")] [SugarColumn(IsNullable = true,OldColumnName = "Dob")]
public double? Dob2 { get; set; } public double? Dob2 { get; set; }
[SugarColumn(Length =10)] [SugarColumn(Length =110)]
public string A { get; set; } public string A1 { get; set; }
} }
public class CodeTable2 { public class CodeTable2 {
public int Id { get; set; } public int Id { get; set; }

View File

@@ -112,7 +112,7 @@ namespace SqlSugar
.Where(ec => !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase))) .Where(ec => !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
.Where(ec => .Where(ec =>
dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName) dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName)
&& ((ec.Length != dc.Length && PubMethod.GetUnderType(ec.PropertyInfo).IsIn(PubConst.StringType)) || && ((ec.Length != dc.Length &&!PubMethod.GetUnderType(ec.PropertyInfo).IsEnum&& PubMethod.GetUnderType(ec.PropertyInfo).IsIn(PubConst.StringType)) ||
ec.IsNullable != dc.IsNullable || ec.IsNullable != dc.IsNullable ||
IsSamgeType(ec, dc)))).ToList(); IsSamgeType(ec, dc)))).ToList();
var renameColumns = entityColumns var renameColumns = entityColumns
@@ -235,9 +235,18 @@ namespace SqlSugar
protected virtual bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc) protected virtual bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc)
{ {
var propType = this.Context.Ado.DbBind.GetDbTypeName(PubMethod.GetUnderType(ec.PropertyInfo).Name); var propertyType = PubMethod.GetUnderType(ec.PropertyInfo);
var properyTypeName = string.Empty;
if (propertyType.IsEnum)
{
properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(ec.Length > 9 ? PubConst.LongType.Name : PubConst.IntType.Name);
}
else
{
properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(propertyType.Name);
}
var dataType = dc.DataType; var dataType = dc.DataType;
return propType != dataType; return properyTypeName != dataType;
} }
#endregion #endregion
} }

View File

@@ -31,12 +31,12 @@ namespace SqlSugar
public virtual List<DbColumnInfo> GetColumnInfosByTableName(string tableName) public virtual List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
{ {
if (string.IsNullOrEmpty(tableName)) return new List<DbColumnInfo>(); if (string.IsNullOrEmpty(tableName)) return new List<DbColumnInfo>();
string key = "DbMaintenanceProvider.GetColumnInfosByTableName." + tableName.ToLower(); string key = "DbMaintenanceProvider.GetColumnInfosByTableName." +this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
return GetListOrCache<DbColumnInfo>(key, string.Format(this.GetColumnInfosByTableNameSql, tableName)); return GetListOrCache<DbColumnInfo>(key, string.Format(this.GetColumnInfosByTableNameSql, tableName));
} }
public virtual List<string> GetIsIdentities(string tableName) public virtual List<string> GetIsIdentities(string tableName)
{ {
string cacheKey = "DbMaintenanceProvider.GetIsIdentities" + tableName.ToLower(); string cacheKey = "DbMaintenanceProvider.GetIsIdentities" +this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
return this.Context.RewritableMethods.GetCacheInstance<List<string>>().Func(cacheKey, return this.Context.RewritableMethods.GetCacheInstance<List<string>>().Func(cacheKey,
(cm, key) => (cm, key) =>
{ {
@@ -50,7 +50,7 @@ namespace SqlSugar
} }
public virtual List<string> GetPrimaries(string tableName) public virtual List<string> GetPrimaries(string tableName)
{ {
string cacheKey = "DbMaintenanceProvider.GetPrimaries" + tableName.ToLower(); string cacheKey = "DbMaintenanceProvider.GetPrimaries" +this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
return this.Context.RewritableMethods.GetCacheInstance<List<string>>().Func(cacheKey, return this.Context.RewritableMethods.GetCacheInstance<List<string>>().Func(cacheKey,
(cm, key) => (cm, key) =>
{ {
@@ -67,12 +67,15 @@ namespace SqlSugar
#region Check #region Check
public virtual bool IsAnyTable(string tableName) public virtual bool IsAnyTable(string tableName)
{ {
tableName = this.SqlBuilder.GetNoTranslationColumnName(tableName);
var tables = GetTableInfoList(); var tables = GetTableInfoList();
if (tables == null) return false; if (tables == null) return false;
else return tables.Any(it => it.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase)); else return tables.Any(it => it.Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase));
} }
public virtual bool IsAnyColumn(string tableName, string columnName) public virtual bool IsAnyColumn(string tableName, string columnName)
{ {
columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
var isAny = IsAnyTable(tableName); var isAny = IsAnyTable(tableName);
Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName)); Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
var columns = GetColumnInfosByTableName(tableName); var columns = GetColumnInfosByTableName(tableName);
@@ -81,6 +84,7 @@ namespace SqlSugar
} }
public virtual bool IsPrimaryKey(string tableName, string columnName) public virtual bool IsPrimaryKey(string tableName, string columnName)
{ {
columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
var isAny = IsAnyTable(tableName); var isAny = IsAnyTable(tableName);
Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName)); Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
var columns = GetColumnInfosByTableName(tableName); var columns = GetColumnInfosByTableName(tableName);
@@ -89,6 +93,7 @@ namespace SqlSugar
} }
public virtual bool IsIdentity(string tableName, string columnName) public virtual bool IsIdentity(string tableName, string columnName)
{ {
columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
var isAny = IsAnyTable(tableName); var isAny = IsAnyTable(tableName);
Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName)); Check.Exception(!isAny, string.Format("Table {0} does not exist", tableName));
var columns = GetColumnInfosByTableName(tableName); var columns = GetColumnInfosByTableName(tableName);
@@ -117,47 +122,57 @@ namespace SqlSugar
#region DDL #region DDL
public virtual bool AddPrimaryKey(string tableName, string columnName) public virtual bool AddPrimaryKey(string tableName, string columnName)
{ {
string sql = string.Format(this.AddPrimaryKeySql,this.SqlBuilder.GetTranslationTableName(tableName), string.Format("PK_{0}_{1}", tableName, columnName), columnName); tableName = this.SqlBuilder.GetTranslationTableName(tableName);
columnName=this.SqlBuilder.GetTranslationTableName(columnName);
string sql = string.Format(this.AddPrimaryKeySql,tableName, string.Format("PK_{0}_{1}",this.SqlBuilder.GetNoTranslationColumnName(tableName), this.SqlBuilder.GetNoTranslationColumnName(columnName)), columnName);
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
} }
public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo) public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo)
{ {
string sql = GetAddColumnSql(this.SqlBuilder.GetTranslationTableName(tableName), columnInfo); tableName = this.SqlBuilder.GetTranslationTableName(tableName);
string sql = GetAddColumnSql(tableName, columnInfo);
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
} }
public virtual bool UpdateColumn(string tableName, DbColumnInfo column) public virtual bool UpdateColumn(string tableName, DbColumnInfo column)
{ {
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
string sql = GetUpdateColumnSql(tableName, column); string sql = GetUpdateColumnSql(tableName, column);
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
} }
public virtual bool CreateTable(string tableName, List<DbColumnInfo> columns) public virtual bool CreateTable(string tableName, List<DbColumnInfo> columns)
{ {
string sql = GetCreateTableSql(this.SqlBuilder.GetTranslationTableName(tableName), columns); tableName = this.SqlBuilder.GetTranslationTableName(tableName);
string sql = GetCreateTableSql(tableName, columns);
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
} }
public virtual bool DropTable(string tableName) public virtual bool DropTable(string tableName)
{ {
this.Context.Ado.ExecuteCommand(string.Format(this.DropTableSql, this.SqlBuilder.GetTranslationTableName(tableName))); tableName = this.SqlBuilder.GetTranslationTableName(tableName);
this.Context.Ado.ExecuteCommand(string.Format(this.DropTableSql,tableName));
return true; return true;
} }
public virtual bool DropColumn(string tableName, string columnName) public virtual bool DropColumn(string tableName, string columnName)
{ {
columnName = this.SqlBuilder.GetTranslationColumnName(columnName);
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
this.Context.Ado.ExecuteCommand(string.Format(this.DropColumnToTableSql, tableName, columnName)); this.Context.Ado.ExecuteCommand(string.Format(this.DropColumnToTableSql, tableName, columnName));
return true; return true;
} }
public virtual bool DropConstraint(string tableName, string constraintName) public virtual bool DropConstraint(string tableName, string constraintName)
{ {
string sql = string.Format(this.DropConstraintSql, this.SqlBuilder.GetTranslationTableName(tableName), constraintName); tableName = this.SqlBuilder.GetTranslationTableName(tableName);
string sql = string.Format(this.DropConstraintSql, tableName, constraintName);
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
} }
public virtual bool TruncateTable(string tableName) public virtual bool TruncateTable(string tableName)
{ {
this.Context.Ado.ExecuteCommand(string.Format(this.TruncateTableSql, this.SqlBuilder.GetTranslationTableName(tableName))); tableName = this.SqlBuilder.GetTranslationTableName(tableName);
this.Context.Ado.ExecuteCommand(string.Format(this.TruncateTableSql, tableName));
return true; return true;
} }
public virtual bool BackupDataBase(string databaseName, string fullFileName) public virtual bool BackupDataBase(string databaseName, string fullFileName)
@@ -172,12 +187,17 @@ namespace SqlSugar
} }
public virtual bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue) public virtual bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue)
{ {
string sql = string.Format(this.BackupTableSql, maxBackupDataRows, newTableName, oldTableName); oldTableName = this.SqlBuilder.GetTranslationTableName(oldTableName);
newTableName = this.SqlBuilder.GetTranslationTableName(newTableName);
string sql = string.Format(this.BackupTableSql, maxBackupDataRows,newTableName , oldTableName);
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
} }
public virtual bool RenameColumn(string tableName, string oldColumnName, string newColumnName) public virtual bool RenameColumn(string tableName, string oldColumnName, string newColumnName)
{ {
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
oldColumnName = this.SqlBuilder.GetTranslationColumnName(oldColumnName);
newColumnName = this.SqlBuilder.GetTranslationColumnName(newColumnName);
string sql = string.Format(this.RenameColumnSql, tableName, oldColumnName, newColumnName); string sql = string.Format(this.RenameColumnSql, tableName, oldColumnName, newColumnName);
this.Context.Ado.ExecuteCommand(sql); this.Context.Ado.ExecuteCommand(sql);
return true; return true;
@@ -207,13 +227,13 @@ namespace SqlSugar
Check.Exception(columns.IsNullOrEmpty(), "No columns found "); Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
foreach (var item in columns) foreach (var item in columns)
{ {
string columnName = item.DbColumnName; string columnName =this.SqlBuilder.GetTranslationTableName(item.DbColumnName);
string dataType = item.DataType; string dataType = item.DataType;
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null; string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull; string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
string primaryKey = null; string primaryKey = null;
string identity = item.IsIdentity ? this.CreateTableIdentity : null; string identity = item.IsIdentity ? this.CreateTableIdentity : null;
string addItem = string.Format(this.CreateTableColumn,this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity); string addItem = string.Format(this.CreateTableColumn,columnName, dataType, dataSize, nullType, primaryKey, identity);
columnArray.Add(addItem); columnArray.Add(addItem);
} }
string tableString = string.Format(this.CreateTableSql,this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray)); string tableString = string.Format(this.CreateTableSql,this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
@@ -221,7 +241,8 @@ namespace SqlSugar
} }
protected virtual string GetAddColumnSql(string tableName, DbColumnInfo columnInfo) protected virtual string GetAddColumnSql(string tableName, DbColumnInfo columnInfo)
{ {
string columnName = columnInfo.DbColumnName; string columnName=this.SqlBuilder.GetTranslationColumnName(columnInfo.DbColumnName);
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
string dataType = columnInfo.DataType; string dataType = columnInfo.DataType;
string dataSize = columnInfo.Length > 0 ? string.Format("({0})", columnInfo.Length) : null; string dataSize = columnInfo.Length > 0 ? string.Format("({0})", columnInfo.Length) : null;
string nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull; string nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull;
@@ -232,7 +253,8 @@ namespace SqlSugar
} }
protected virtual string GetUpdateColumnSql(string tableName, DbColumnInfo columnInfo) protected virtual string GetUpdateColumnSql(string tableName, DbColumnInfo columnInfo)
{ {
string columnName = columnInfo.DbColumnName; string columnName =this.SqlBuilder.GetTranslationTableName(columnInfo.DbColumnName);
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
string dataType = columnInfo.DataType; string dataType = columnInfo.DataType;
string dataSize = columnInfo.Length > 0 ? string.Format("({0})", columnInfo.Length) : null; string dataSize = columnInfo.Length > 0 ? string.Format("({0})", columnInfo.Length) : null;
string nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull; string nullType = columnInfo.IsNullable ? this.CreateTableNull : CreateTableNotNull;

View File

@@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.0.4")] [assembly: AssemblyVersion("4.0.6")]
[assembly: AssemblyFileVersion("4.0.4")] [assembly: AssemblyFileVersion("4.0.6")]

View File

@@ -37,11 +37,14 @@ namespace SqlSugar
public override string GetTranslationColumnName(string propertyName) public override string GetTranslationColumnName(string propertyName)
{ {
return "[" + propertyName + "]"; if (propertyName.Contains("[")) return propertyName;
else
return "[" + propertyName + "]";
} }
public override string GetNoTranslationColumnName(string name) public override string GetNoTranslationColumnName(string name)
{ {
if (!name.Contains("[")) return name;
return name == null ? string.Empty : Regex.Match(name, @"\[(.*?)\]").Groups[1].Value; return name == null ? string.Empty : Regex.Match(name, @"\[(.*?)\]").Groups[1].Value;
} }
} }

View File

@@ -2,7 +2,7 @@
<package > <package >
<metadata> <metadata>
<id>sqlSugar</id> <id>sqlSugar</id>
<version>4.0.4</version> <version>4.0.5</version>
<title>SqlSugar SqlServer ORM</title> <title>SqlSugar SqlServer ORM</title>
<authors>sun kaixuan</authors> <authors>sun kaixuan</authors>
<owners>landa</owners> <owners>landa</owners>