From e5be375308a516536bb24dd2d43404ec55c2110f Mon Sep 17 00:00:00 2001 From: hang <87227557@qq.com> Date: Fri, 13 Sep 2024 21:45:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=AE=9E=E4=BD=93=E7=9A=84=E6=A8=A1=E5=9E=8B=EF=BC=8C=E9=95=BF?= =?UTF-8?q?=E5=BA=A6200=E7=9A=84=E4=B8=8D=E6=A0=87=E6=B3=A8=EF=BC=8Cnvarch?= =?UTF-8?q?armax=20=E6=A0=87=E6=B3=A8=20MaxLength=20=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E6=A0=87=E6=B3=A8StringLength?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CodeTemplates/EFCore/EntityType.t4 | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/IRaCIS.Core.Test/CodeTemplates/EFCore/EntityType.t4 b/IRaCIS.Core.Test/CodeTemplates/EFCore/EntityType.t4 index 65b856ee7..6246b3f15 100644 --- a/IRaCIS.Core.Test/CodeTemplates/EFCore/EntityType.t4 +++ b/IRaCIS.Core.Test/CodeTemplates/EFCore/EntityType.t4 @@ -49,7 +49,7 @@ namespace <#= NamespaceHint #>; if (!string.IsNullOrEmpty(EntityType.GetComment())) { #> -[comment("<#= code.XmlComment(EntityType.GetComment()) #>")] +[Comment("<#= code.XmlComment(EntityType.GetComment()) #>")] <# } @@ -86,8 +86,32 @@ public partial class <#= EntityType.Name #>: BaseFullAuditEntity if (Options.UseDataAnnotations) { - var dataAnnotations = property.GetDataAnnotations(annotationCodeGenerator) - .Where(a => !(a.Type == typeof(RequiredAttribute) && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType)); + // 检查属性是否为字符串类型,并根据MaxLength来决定是否生成[StringLength]注解 + if (property.ClrType == typeof(string)) + { + var maxLength = property.GetMaxLength(); + + if (maxLength.HasValue && maxLength != 200) // 仅当长度不为200时生成[StringLength] + { + + +#> + [StringLength(<#= maxLength.Value #>)] +<# + + + + } + else + { +#> + [MaxLength] +<# + } + } + var dataAnnotations = property.GetDataAnnotations(annotationCodeGenerator) + .Where(a => !(a.Type == typeof(RequiredAttribute) && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType) + && a.Type != typeof(StringLengthAttribute)); // 排除 StringLengthAttribute foreach (var dataAnnotation in dataAnnotations) { #>