You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.4 KiB
C#

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 System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DSWeb.Common.DB
{
/// <summary>
/// <para>自定义Decimal类型的精度属性</para>
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class DecimalPrecisionAttribute : Attribute
{
#region Field
private byte _precision = 18;
public byte _scale = 4;
#endregion
#region Construct
/// <summary>
/// <para>自定义Decimal类型的精确度属性</para>
/// </summary>
/// <param name="precision">precision
/// <para>精度默认18</para></param>
/// <param name="scale">scale
/// <para>小数位数默认4</para></param>
public DecimalPrecisionAttribute(byte precision = 18, byte scale = 4)
{
Precision = precision;
Scale = scale;
}
#endregion
#region Property
/// <summary>
/// 精确度默认18
/// </summary>
public byte Precision
{
get { return this._precision; }
set { this._precision = value; }
}
/// <summary>
/// 保留位数默认4
/// </summary>
public byte Scale
{
get { return this._scale; }
set { this._scale = value; }
}
#endregion
}
/// <summary>
/// 用于modelBuilder全局设置自定义精度属性
/// </summary>
public class DecimalPrecisionAttributeConvention
: PrimitivePropertyAttributeConfigurationConvention<DecimalPrecisionAttribute>
{
public override void Apply(ConventionPrimitivePropertyConfiguration configuration, DecimalPrecisionAttribute attribute)
{
if (attribute.Precision < 1 || attribute.Precision > 38)
{
throw new InvalidOperationException("Precision must be between 1 and 38.");
}
if (attribute.Scale > attribute.Precision)
{
throw new InvalidOperationException("Scale must be between 0 and the Precision value.");
}
configuration.HasPrecision(attribute.Precision, attribute.Scale);
}
}
}