37 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C#
		
	
	
| using Microsoft.EntityFrameworkCore.Metadata.Builders;
 | ||
| using Microsoft.EntityFrameworkCore.Metadata.Conventions;
 | ||
| 
 | ||
| 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);
 | ||
|         }
 | ||
|     }
 | ||
| }
 |