EFCore通过Include关联清单不存在时返回值为默认值的方式

背景

  最近在使用EFCore2.1写代码的时候遇到一个问题,在最终的查询结果中有一个SinglePrice字段查询的结果总是不符合预期,按照自己的设想这个字段是主单的一个TotalFeeAfter字段和主单关联的清单其中的CouponFee合计这两者之间的差值,当然主单关联的清单可能不存在,所以当清单不存在时清单的CouponFee合计值为0,可是在使用EFCore写的代码中结果返回的总是null,显然这个不符合预期,为了交代好这一个过程,我们来看一下这个主清单BO,从而便于自己有一个主观的认知。

  1.1 主单保养套餐销售订单(MaintenancePackageOrder)

/// <summary>
    /// 保养套餐销售订单
    /// </summary>
    [Table("MaintenancePackageOrder")]
    public class MaintenancePackageOrder : RowVersionAuditEntity, IMustHaveCode {
        public MaintenancePackageOrder() {           
            Coupons = new List<MaintenancePackageOrderCoupon>();
        }       
        /// <summary>
        ///订单编号
        /// </summary>
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string Code { get; set; }      
        /// <summary>
        ///营销分公司编号
        /// </summary>
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string BranchCode { get; set; }       
      
        /// <summary>
        ///品牌编号
        /// </summary>
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string BrandCode { get; set; }      
      
        /// <summary>
        ///仓库编号
        /// </summary>
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string WarehouseCode { get; set; }
        /// <summary>
        ///仓库名称
        /// </summary>
        [Required]
        [MaxLength(EntityDefault.FieldLength_100)]
        public string WarehouseName { get; set; }      
        /// <summary>
        ///车辆售后档案
        /// </summary>
        public Guid VehicleSoldId { get; set; }
        /// <summary>
        ///VIN
        /// </summary>
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string Vin { get; set; }
        /// <summary>
        ///产品编号
        /// </summary>
        [MaxLength(EntityDefault.FieldLength_50)]
        public string ProductCode { get; set; }
        /// <summary>
        ///车牌号
        /// </summary>
        [MaxLength(EntityDefault.FieldLength_50)]
        public string LicensePlate { get; set; }      
        /// <summary>
        ///行驶里程
        /// </summary>
        public int? Mileage { get; set; }
        /// <summary>
        ///订单总金额
        /// </summary>
        [Required]
        public decimal TotalFee { get; set; }
        /// <summary>
        ///优惠后总金额
        /// </summary>
        public decimal TotalFeeAfter { get; set; }
        /// <summary>
        ///状态
        /// </summary>
        public MaintenancePackageOrderStatus Status { get; set; }      
     
        /// <summary>
        ///付款方式
        /// </summary>
        public PartRetailOrderPaymentType? PaymentType { get; set; }
        /// <summary>
        ///发票号
        /// </summary>
        [MaxLength(EntityDefault.FieldLength_100)]
        public string InvoiceNumber { get; set; }
        /// <summary>
        ///备注
        /// </summary>
        [MaxLength(EntityDefault.FieldLength_200)]
        public string Remark { get; set; }   
        /// <summary>
        /// 产品分类id
        /// </summary>
        public Guid? ProductCategoryId { get; set; }

        /// <summary>
        /// 产品分类id
        /// </summary>
        [MaxLength(EntityDefault.FieldLength_50)]
        public string ProductCategoryCode { get; set; }  

        /// <summary>
        /// 销售顾问
        /// </summary>
        [MaxLength(EntityDefault.FieldLength_100)]
        public string SaleAdviserName { get; set; }   

        /// <summary>
        /// 保养套餐销售订单抵用券清单(DMSPart)
        /// </summary>
        public List<MaintenancePackageOrderCoupon> Coupons { get; set; }
    }

  1.2 清单保养套餐抵用券清单(MaintenancePackageOrderCoupon)

/// <summary>
    /// 保养套餐销售订单抵用券清单(DMSPart)
    /// </summary>
    public class MaintenancePackageOrderCoupon : Entity<Guid> {
        /// <summary>
        /// 保养套餐销售订单
        /// </summary>
        [ForeignKeyReference(DeleteBehavior = DeleteBehavior.Cascade)]
        public Guid MaintenancePackageOrderId { get; set; }

        /// <summary>
        /// 代金券
        /// </summary>
        public Guid? CouponId { get; set; }

        /// <summary>
        /// 抵用券编号
        /// </summary>
        [MaxLength(EntityDefault.FieldLength_100)]
        public string CouponCode { get; set; }

        /// <summary>
        /// 抵用券面值
        /// </summary>
        public decimal? CouponPrice { get; set; }

        /// <summary>
        /// 抵用金额
        /// </summary>
        public decimal? CouponFee { get; set; }

    }

  这里只是为了解释主单MaintenancePackageOrder和清单MaintenancePackageOrderCoupon一对多的关系,所以这里只截取了部分的字段名称。

上一篇:LeetCode | 67. 二进制求和


下一篇:Leetcode 最长回文子串