irc-netcore-api/IRaCIS.Core.Infra.EFCore/Context/Convention/DecimalPrecisionConvention.cs

41 lines
1.2 KiB
C#

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IRaCIS.Core.Infra.EFCore;
public class DecimalPrecisionConvention : IModelFinalizingConvention
{
private readonly int _precision;
private readonly int _scale;
public DecimalPrecisionConvention(int precision, int scale)
{
_precision = precision;
_scale = scale;
}
public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
{
foreach (var property in modelBuilder.Metadata.GetEntityTypes()
.SelectMany(
entityType => entityType.GetDeclaredProperties()
.Where(
property => property.ClrType == typeof(decimal) || property.ClrType == typeof(decimal?))))
{
// 设置精度和小数位数
property.SetPrecision(_precision);
property.SetScale(_scale);
}
}
}