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) { #>