diff --git a/FreeSql.Tests/FreeSql.Tests/Dameng/DamengCodeFirstTest.cs b/FreeSql.Tests/FreeSql.Tests/Dameng/DamengCodeFirstTest.cs index 01dbc0221..481b51094 100644 --- a/FreeSql.Tests/FreeSql.Tests/Dameng/DamengCodeFirstTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/Dameng/DamengCodeFirstTest.cs @@ -454,5 +454,74 @@ class TableAllType public enum TableAllTypeEnumType1 { e1, e2, e3, e5 } [Flags] public enum TableAllTypeEnumType2 { f1, f2, f3 } + + + + [Fact] + public void UpgradeTableStructure() + { + g.dameng.CodeFirst.SyncStructure(); + + g.dameng.Delete() + .Where(item => 1 == 1) + .ExecuteAffrows(); + + var text = new StringBuilder(); + var temp = "1234567890哈"; + for (int i = 0; i < 3000 - temp.Length; i+= temp.Length) + { + text.Append(temp); + } + + var mode = new TableUpgradeTest + { + Name = "1234560123456012哈哈哈哈哈呵呵_123123", + Text = text.ToString(), + Text2 = "呵呵呵Test123123123" + }; + + mode.Id = g.dameng.Insert(mode) + .ExecuteIdentity(); + + // 第二次进行数据库结果同步 + var sql = g.dameng.CodeFirst.GetComparisonDDLStatements(); + g.dameng.Ado.ExecuteNonQuery(sql); + //g.dameng.CodeFirst.SyncStructure(); + + var resultMode = g.dameng.Select() + .Where(item => item.Id == mode.Id) + .First(); + + Assert.Equal(mode.Name, resultMode?.Name); + Assert.Equal(mode.Text, resultMode?.Text); + Assert.Equal(mode.Text2, resultMode?.Text2); + Assert.Equal(mode.Text3, resultMode?.Text3); + + } + + [Table(Name = "tb_upgrade_test")] + public class TableUpgradeTest + { + [Column(Name = "id", IsPrimary = true, IsIdentity = true)] + public long Id { get; set; } + + + [Column(Name = "name", StringLength = 31, IsNullable = false)] + public string Name { get; set; } + + [Column(Name = "text", StringLength = 32767, IsNullable = false)] + public string Text { get; set; } + + [Column(Name = "number_test", IsNullable = false)] + public int TestNumber { get; set; } + + [Column(Name = "text2", StringLength = 63)] + public string Text2 { get; set; } + + + [Column(Name = "text3")] + public string Text3 { get; set; } + } + } } diff --git a/Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs b/Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs index 664ddb376..f6142938e 100644 --- a/Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs +++ b/Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs @@ -358,6 +358,17 @@ and a.owner in ({0}) and a.table_name in ({1}) if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false) { var dbtypeNoneNotNull = Regex.Replace(tbcol.Attribute.DbType, @"(NOT\s+)?NULL", ""); + + var charMatch = Regex.Match(dbtypeNoneNotNull, "(N?)VARCHAR(2?)\\((?[0-9]+)\\)"); + + if(charMatch != null) + { + if (ushort.TryParse(charMatch.Groups["precision"]?.Value, out var precision)) + { + dbtypeNoneNotNull = Regex.Replace(dbtypeNoneNotNull, $"\\(({precision})\\)", $""); + } + } + insertvalue = $"cast({insertvalue} as {dbtypeNoneNotNull})"; } if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable)