自定义生成实体的模型,长度200的不标注,nvarcharmax 标注 MaxLength 其他长度标注StringLength
continuous-integration/drone/push Build is passing Details

IRC_NewDev
hang 2024-09-13 21:45:42 +08:00
parent 095db5786b
commit e5be375308
1 changed files with 27 additions and 3 deletions

View File

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