详解EFCore中的导航属性

  利用了这么久的EntityFrameworkCore框架,本日想来就个中的一个部门来做一个常识的梳理,从而使本身对付整个常识有一个越发深入的领略,假如你对EFCore中的实体干系不熟悉你需要有一个常识的预热,这样你才气够更好的去领略整个常识,在成立好了这些实体之间的干系今后,我们可以通过利用InClude、ThenInclude这些要领来举办快速得到对应关联实体数据,用起来确实十分的利便,这里我们将通过一系列的例子来举办说明。

    1 单独利用Include

  在先容这个要领之前,我来先贴出实体之间的关联干系,假设这里有三个彼此关联的实体VehicleWarranty、WarrantyWarningLevel、VehicleWarrantyRepairHistory这三个实体后头两个都是第一个的子级而且而且VehicleWarranty、WarrantyWarningLevel之间的干系是1对1的干系,VehicleWarranty和VehicleWarrantyRepairHistory之间是1:N的干系,即1对多的干系,我们这里贴出详细的Model,从而利便后头阐明详细的代码。

    /// <summary>     /// 车辆三包信息(DCSService)     /// </summary>     public class VehicleWarranty : Entity<Guid> {         public VehicleWarranty() {             Details = new List<VehicleWarrantyRepairHistory>();         }           //车辆售后档案         public Guid VehicleSoldId { get; set; }           //VIN         [Required]         [MaxLength(EntityDefault.FieldLength_50)]         public string Vin { get; set; }           //产物分类         public Guid? ProductCategoryId { get; set; }           //产物分类编号         [MaxLength(EntityDefault.FieldLength_50)]         public string ProductCategoryCode { get; set; }           //产物分类名称         [MaxLength(EntityDefault.FieldLength_100)]         public string ProductCategoryName { get; set; }           //车牌号         [MaxLength(EntityDefault.FieldLength_50)]         public string LicensePlate { get; set; }           //动员机号         [MaxLength(EntityDefault.FieldLength_50)]         public string EngineCode { get; set; }           //变速箱号         [MaxLength(EntityDefault.FieldLength_50)]         public string TransmissionSn { get; set; }           //开拓票日期         public DateTime InvoiceDate { get; set; }           //行驶里程         public int Mileage { get; set; }           //是否三包期内         public bool? IsInWarranty { get; set; }           //预警品级         public Guid? WarningLevelId { get; set; }           public WarrantyWarningLevel WarningLevel { get; set; }           //品级编号         [MaxLength(EntityDefault.FieldLength_50)]         public string LevelCode { get; set; }           //品级名称         [MaxLength(EntityDefault.FieldLength_100)]         public string LevelName { get; set; }           //预警内容         [MaxLength(EntityDefault.FieldLength_800)]         public string WarningComment { get; set; }           //累计维修天数         public int TotoalRepairDays { get; set; }           //售出后60天/3000KM内严重妨碍次数         public int? FNum { get; set; }           //严重安详机能妨碍累计次数         public int? GNum { get; set; }           //动员机总成累计改换次数         public int? HNum { get; set; }           //变速箱总成累计改换次数         public int? INum { get; set; }           //动员机主要零件最大改换次数         public int? JNum { get; set; }           //变速箱主要零件最大改换次数         public int? KNum { get; set; }           //同一主要零件最大改换次数         public int? LNum { get; set; }           //同一产物质量问题最大累计次数(部件+妨碍+方位)         public int? MNum { get; set; }           //同一产物质量问题最大累计次数         public int? NNum { get; set; }           public List<VehicleWarrantyRepairHistory> Details { get; set; }     }   /// <summary>     /// 三包预警品级(DCS)     /// </summary>     public class WarrantyWarningLevel : Entity<Guid> {         //品级编号         [Required]         [MaxLength(EntityDefault.FieldLength_50)]         public string Code { get; set; }           //品级名称         [Required]         [MaxLength(EntityDefault.FieldLength_100)]         public string Name { get; set; }           //颜色         [Required]         [MaxLength(EntityDefault.FieldLength_50)]         public string Color { get; set; }           //备注         [MaxLength(EntityDefault.FieldLength_200)]         public string Remark { get; set; }     }         /// <summary>     /// 车辆三包信息维修经历(DCSService)     /// </summary>     public class VehicleWarrantyRepairHistory : Entity<Guid> {         //车辆三包信息         [Required]         public Guid VehicleWarrantyId { get; set; }           public VehicleWarranty VehicleWarranty { get; set; }           //VIN         [Required]         [MaxLength(EntityDefault.FieldLength_50)]         public string Vin { get; set; }           //维修条约         public Guid RepairContractId { get; set; }           //维修条约编号         [Required]         [MaxLength(EntityDefault.FieldLength_50)]         public string RepairContractCode { get; set; }           //处理惩罚时间         public DateTime? DealTime { get; set; }           //经销商         public Guid DealerId { get; set; }           //经销商编号         [Required]         [MaxLength(EntityDefault.FieldLength_50)]         public string DealerCode { get; set; }           //经销商名称         [Required]         [MaxLength(EntityDefault.FieldLength_100)]         public string DealerName { get; set; }           //经向来历         public VehicleWarrantyRepairHistorySource Source { get; set; }           //累计维修天数         public int? TotoalRepairDays { get; set; }           //售出后60天/3000KM内严重妨碍次数         public int? FNum { get; set; }           //严重安详机能妨碍累计次数         public int? GNum { get; set; }           //动员机总成累计改换次数         public int? HNum { get; set; }           //变速箱总成累计改换次数         public int? INum { get; set; }           //动员机主要零件最大改换次数         public int? JNum { get; set; }           //变速箱主要零件最大改换次数         public int? KNum { get; set; }           //同一主要零件最大改换次数         public int? LNum { get; set; }           //同一产物质量问题最大累计次数(部件+妨碍+方位)         public int? MNum { get; set; }           //同一产物质量问题最大累计次数         public int? NNum { get; set; }     }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsjfyg.html