From a4e9026de54ccdcdc5faf262777acf3a3b7236b5 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 25 Jun 2017 06:32:33 +0800 Subject: [PATCH] Update MySql --- Src/Asp.Net/MySqlTest/Demos/5_CodeFirst.cs | 2 +- .../CodeFirstProvider/CodeFirstProvider.cs | 19 ++++++++++++------- .../MySql/CodeFirst/MySqlCodeFirst.cs | 17 ++++++++++++++--- .../MySql/DbMaintenance/MySqlDbMaintenance.cs | 4 ++-- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Src/Asp.Net/MySqlTest/Demos/5_CodeFirst.cs b/Src/Asp.Net/MySqlTest/Demos/5_CodeFirst.cs index e5ba80d82..e56b212e5 100644 --- a/Src/Asp.Net/MySqlTest/Demos/5_CodeFirst.cs +++ b/Src/Asp.Net/MySqlTest/Demos/5_CodeFirst.cs @@ -13,7 +13,7 @@ namespace OrmTest.Demo public int Id { get; set; } [SugarColumn(Length = 21,OldColumnName = "Name2")] public string Name{ get; set; } - [SugarColumn(IsNullable = true,Length =10)] + [SugarColumn(IsNullable = true,Length =11)] public string IsOk { get; set; } public Guid Guid { get; set; } public decimal Decimal { get; set; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 5c7f6eabb..2c17ec836 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -163,13 +163,7 @@ namespace SqlSugar } else if (pkDiff || idEntityDiff) { - string constraintName = string.Format("PK_{0}_{1}", tableName, item.DbColumnName); - if (this.Context.DbMaintenance.IsAnyConstraint(constraintName)) - this.Context.DbMaintenance.DropConstraint(tableName, constraintName); - this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName); - this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item)); - if (item.IsPrimarykey) - this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName); + ChangeKey(entityInfo, tableName, item); } } if (isChange && IsBackupTable) @@ -179,6 +173,17 @@ namespace SqlSugar } } + protected virtual void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item) + { + string constraintName = string.Format("PK_{0}_{1}", tableName, item.DbColumnName); + if (this.Context.DbMaintenance.IsAnyConstraint(constraintName)) + this.Context.DbMaintenance.DropConstraint(tableName, constraintName); + this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName); + this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item)); + if (item.IsPrimarykey) + this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName); + } + protected virtual void ConvertColumns(List dbColumns) { diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs index 02c1cdc80..38ac994da 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/CodeFirst/MySqlCodeFirst.cs @@ -5,7 +5,7 @@ using System.Text; namespace SqlSugar { - public class MySqlCodeFirst:CodeFirstProvider + public class MySqlCodeFirst : CodeFirstProvider { public override void NoExistLogic(EntityInfo entityInfo) { @@ -37,7 +37,8 @@ namespace SqlSugar ColumnDescription = item.ColumnDescription, Length = item.Length }; - if (result.DataType.Equals("varchar",StringComparison.CurrentCultureIgnoreCase)&& result.Length == 0) { + if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0) + { result.Length = 1; } return result; @@ -47,10 +48,20 @@ namespace SqlSugar { foreach (var item in dbColumns) { - if (item.DataType == "DateTime") { + if (item.DataType == "DateTime") + { item.Length = 0; } } } + + protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item) + { + this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item)); + if (!item.IsPrimarykey) + this.Context.DbMaintenance.DropConstraint(tableName,null); + if (item.IsPrimarykey) + this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName); + } } } diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs index c7fa9cb76..4e6f21a62 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/DbMaintenance/MySqlDbMaintenance.cs @@ -104,7 +104,7 @@ namespace SqlSugar { get { - return "SELECT TOP {0} * INTO {1} FROM {2}"; + return "SELECT * INTO {1} FROM {2} limit 0,{0}"; } } protected override string DropTableSql @@ -125,7 +125,7 @@ namespace SqlSugar { get { - return "ALTER TABLE {0} DROP CONSTRAINT {1}"; + return "ALTER TABLE {0} drop primary key;"; } } protected override string RenameColumnSql