详解EFCore中的导航属性(7)

internal class AddRepairContractEventManager : DomainService, IAddRepairContractEventManager {         private readonly KafkaProducer _producer;         private readonly IRepository<RepairContract, Guid> _repairContractRepository;         private readonly IRepository<RepairContractAdjust, Guid> _repairContractAdjustRepository;           public AddRepairContractEventManager(KafkaProducer producer,                                           IRepository<RepairContract, Guid> repairContractRepository,                                           IRepository<RepairContractAdjust, Guid> repairContractAdjustRepository) {             _producer = producer;             _repairContractRepository = repairContractRepository;             _repairContractAdjustRepository = repairContractAdjustRepository;         }           /// <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);         }     }

  4 IncludeFilter用法

  在有些场景中我们大概需要带出清单的时候而且过滤清单,这个成果算是对Include要领的一个晋升,可以将两个操纵归并到一起来举办,这个在利用的时候需要留意 这个并不是Asp.Net Core自带的成果,这个需要通过引入包 Z.EntityFramework.Plus.EFCore.dll的包来实现的,假如你们的系统中利用的是ABP作为项目主框架,那么你只需要引用 Abp.EntityFrameworkCore.EFPlus这个包就可以了,因为这个包中就包括和Z.EntityFramework相关的子包,这个在利用的时候需要留意。

  下面我们来看一看我们的代码中是怎么利用的。

private (Company company, IEnumerable<PartSaleOrderType> partSaleOrderTypes) GetCompanyDetailForFactory(Guid id) {             var currentPartSaleOrderTypes = GetCurrentPartSaleOrderTypes();             var currentPartSaleOrderTypeIds = currentPartSaleOrderTypes.Select(t => t.Id);             var company = _companyRepository.GetAll()                 .IncludeFilter(c => c.CustomerPartInformations.Where(i => i.BranchId == SdtSession.TenantId.GetValueOrDefault()))                 .IncludeFilter(c => c.CustomerOrderWarehouses.Where(w => currentPartSaleOrderTypeIds.Contains(w.PartSaleOrderTypeId)))                 .IncludeFilter(c => c.CustomerMarkupRates.Where(w => currentPartSaleOrderTypeIds.Contains(w.PartSaleOrderTypeId)))                 .IncludeFilter(c => c.OrderShippingSequences.Where(w => currentPartSaleOrderTypeIds.Contains(w.PartSaleOrderTypeId)))                 .IncludeFilter(c => c.OrderingCalendars.Where(w => currentPartSaleOrderTypeIds.Contains(w.PartSaleOrderTypeId)))                 .FirstOrDefault(d => d.Id == id && d.Status == BaseDataStatus.生效);             if (company == null) {                 throw new EntityNotFoundException(SharedLocalizer["未能找到对应的企业"]);             }             return (company, currentPartSaleOrderTypes);         }

  这个提供了一种新的清单过滤方法,不只提高了效率并且使代码越发优化简洁。

  5 非凡环境

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

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