最后还要先容一种极非凡的环境,由于ThenInclude要领只能一层层向下举办,假如我想对同一个实体内里的两个关联实体做ThenInclude操纵这个怎么处理惩罚,这里就直接给出代码吧。
/// <summary> ///维修条约完成的事件 /// </summary> /// <param></param> public void Finished(Guid repairContractId) { var repairContract = _repairContractRepository.GetAll() .Include(c => c.RepairContractWorkItems).ThenInclude(wi => wi.Materials) .Include(c => c.RepairContractWorkItems).ThenInclude(wi => wi.Fault) .SingleOrDefault(c => c.Id == repairContractId); var repairContractAdjusts = _repairContractAdjustRepository.GetAll() .Include(a => a.WorkItems).ThenInclude(w => w.Materials) .Where(a => a.RepairContractId == repairContractId).ToList(); var @event = new AddRepairContractEvent { Key = repairContract?.Code, RepairContract = repairContract, RepairContractAdjusts = repairContractAdjusts }; _producer.Produce(@event); }
这里需要Include同一个实体两次,然后别离挪用ThenInclude要领,这个属于较量非凡的环境,在利用的时候需要留意。
温馨提示:
这里读者在看代码的时候大概不太领略雷同这种 _repairContractRepository的详细由来,这里贴出一份完整的代码。