1、新建DecimalPrecisionAttribute.cs
using System; using System.Collections.Generic; using System.Data.Entity.ModelConfiguration.Configuration; using System.Data.Entity.ModelConfiguration.Conventions; using System.Linq; using System.Web; namespace AppBoxMvc.Models { /// <summary> /// 精度属性控制 /// </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); } } }View Code
2、OnModelCreating新增属性
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约 modelBuilder.Conventions.Remove<IncludeMetadataConvention>();//防止黑幕交易 要不然每次都要访问 modelBuilder.HasDefaultSchema("SCM"); //base.OnModelCreating(modelBuilder); //注册函数 modelBuilder.Conventions.Add(new FunctionsConvention("SCM", this.GetType())); modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention()); }View Code
3、实体属性绑定
using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Example.Models { [Table("T_SCM_QUTV_DET")] public class T_SCM_QUTV_DET { [Key] public string ID { get; set; } [Display(Name = "父ID")] public string T_SCM_QUTV_PARENT { get; set; } [Display(Name = "行号")] public int T_SCM_QUTV_LINE { get; set; } [Display(Name = "单价")] [DecimalPrecision(19, 4)] public decimal T_SCM_QUTV_PRICE { get; set; } [Display(Name = "币种")] public string T_SCM_QUTV_CURR { get; set; } [Display(Name = "币种汇率")] [DecimalPrecision(19, 4)] public decimal T_SCM_QUTV_RATE { get; set; } [Display(Name = "关税税率(%)")] [DecimalPrecision(19, 4)] public decimal T_SCM_QUTV_COT_RATE { get; set; } [Display(Name = "税率(%)")] [DecimalPrecision(19,4)] public decimal T_SCM_QUTV_VAT { get; set; } [Display(Name = "本币币种")] public string T_SCM_QUTV_BASIC_CURR { get; set; } [Display(Name = "本币单价")] [DecimalPrecision(19, 4)] public decimal T_SCM_QUTV_BASIC_PRICE { get; set; } [Display(Name = "税后价格")] [DecimalPrecision(19, 4)] public decimal T_SCM_QUTV_END_PRICE { get; set; } [Display(Name = "备注")] public string T_SCM_QUTV_RMKS { get; set; } [Display(Name = "报价")] public bool T_SCM_QUTV_SUB { get; set; } [Display(Name = "报价人")] public string T_SCM_QUTV_SUB_BY { get; set; } [Display(Name = "报价时间")] public DateTime? T_SCM_QUTV_SUB_DATE { get; set; } [Display(Name = "选取状态")] public bool T_SCM_QUTV_SELETED { get; set; } [Display(Name = "选取人")] public string T_SCM_QUTV_SELETED_BY { get; set; } [Display(Name = "选取时间")] public DateTime? T_SCM_QUTV_SELETED_DATE { get; set; } [Display(Name = "附件")] public string T_SCM_QUTV_FILENAME { get; set; } [Display(Name = "电子路径")] public string T_SCM_QUTV_REALFILENAME { get; set; } [Display(Name = "录入人")] public string T_SCM_QUTV_CRT_BY { get; set; } [Display(Name = "录入时间")] public DateTime T_SCM_QUTV_CRT_DATE { get; set; } [Display(Name = "修改次数")] public int T_SCM_QUTV_MOD_TIMES { get; set; } [Display(Name = "修改人")] public string T_SCM_QUTV_MOD_BY { get; set; } [Display(Name = "修改时间")] public DateTime T_SCM_QUTV_MOD_DATE { get; set; } public virtual T_SCM_QUT_VEND _qut_vend { get; set; } } }View Code