/// <summary> /// Check if a demand exists for the orderpart, else a new one is created. /// </summary> /// <param name="orderPart"></param> /// <returns></returns> public IDemandToProvider GetDemand(OrderPart orderPart) { var demandOrderParts = _context.Demands.OfType <DemandOrderPart>().Include(a => a.DemandProvider).Where(a => a.OrderPartId == orderPart.Id).ToList(); IDemandToProvider demand; if (demandOrderParts.Any()) { demand = demandOrderParts.First(); } else { demand = _context.CreateDemandOrderPart(orderPart); _context.SaveChanges(); } return(demand); }