From e882269122b3b8f0df008016c1c2ef20960664b0 Mon Sep 17 00:00:00 2001 From: xieyidong Date: Wed, 13 Nov 2024 16:54:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E8=BE=BE=E6=A2=A6=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84=E8=A1=A8?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E6=97=B6=E7=94=B1=E4=BA=8Eselect=E4=B8=AD?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=B2=BE=E8=AF=BB=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E8=80=8C=E5=AF=BC=E8=87=B4=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=88=AA=E5=8F=96=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dameng/DamengCodeFirstTest.cs | 69 +++++++++++++++++++ .../DamengCodeFirst.cs | 11 +++ 2 files changed, 80 insertions(+) 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)