decimal 精度解决
continuous-integration/drone/push Build is passing

This commit is contained in:
hang
2024-09-17 18:53:56 +08:00
parent ab68a63bdd
commit 65e47d9918
5 changed files with 145 additions and 70 deletions
@@ -109,11 +109,41 @@ public partial class <#= EntityType.Name #>: BaseFullAuditEntity
<#
}
}
if (property.ClrType == typeof(decimal) || property.ClrType == typeof(decimal?))
{
var typeName = property.GetColumnType()??""; // 获取数据库类型名称
// 匹配 decimal, numeric, 或者 number 格式
var match = System.Text.RegularExpressions.Regex.Match(typeName, @"(decimal|numeric|number)\((\d+),(\d+)\)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (match.Success)
{
var precision = match.Groups[2].Value;
var scale = match.Groups[3].Value;
#>
[DecimalPrecision(<#= precision #>, <#= scale #>)]
<#
}
else
{
#>
[DecimalPrecision(18, 2)] // 默认值
<#
}
}
var dataAnnotations = property.GetDataAnnotations(annotationCodeGenerator)
.Where(a => !(a.Type == typeof(RequiredAttribute) && Options.UseNullableReferenceTypes && !property.ClrType.IsValueType)
&& a.Type != typeof(StringLengthAttribute)); // 排除 StringLengthAttribute
&& a.Type != typeof(StringLengthAttribute) // 排除 StringLengthAttribute
);
foreach (var dataAnnotation in dataAnnotations)
{
//过滤 [Column(TypeName = "decimal(18,1)")] 这种设置TypeName的 直接过滤&& a.Type != typeof(ColumnAttribute) 会报错,很奇怪
if(dataAnnotation.Arguments?.Count==0)
{
continue;
}
#>
<#= code.Fragment(dataAnnotation) #>
<#