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

43 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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