自定义生成实体的模型,长度200的不标注,nvarcharmax 标注 MaxLength 其他长度标注StringLength
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
095db5786b
commit
e5be375308
|
@ -49,7 +49,7 @@ namespace <#= NamespaceHint #>;
|
||||||
if (!string.IsNullOrEmpty(EntityType.GetComment()))
|
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)
|
if (Options.UseDataAnnotations)
|
||||||
{
|
{
|
||||||
var dataAnnotations = property.GetDataAnnotations(annotationCodeGenerator)
|
// 检查属性是否为字符串类型,并根据MaxLength来决定是否生成[StringLength]注解
|
||||||
.Where(a => !(a.Type == typeof(RequiredAttribute) && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType));
|
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)
|
foreach (var dataAnnotation in dataAnnotations)
|
||||||
{
|
{
|
||||||
#>
|
#>
|
||||||
|
|
Loading…
Reference in New Issue