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;
///
/// 设置decimal 默认精度问题 (但是用ef迁移工具的时候,还是会提示No store type was specified for the decimal property,但是迁移文件里面体现了该配置,在dbcontext里写,那么就会没该警告)
///
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 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);
}
}
}