public static string ImportSales(CarDealerContext context, string inputJson) { ICollection salesToImport = new List(); IEnumerable saleDtos = JsonConvert.DeserializeObject(inputJson); if (saleDtos != null) { foreach (ImportSaleDto saleDto in saleDtos) { if (!IsValid(saleDto)) { continue; } bool isCarIdExisting = context .Cars .Any(c => c.Id == saleDto.CarId); if (!isCarIdExisting) { continue; } bool isCustomerIdExisting = context .Customers .Any(cu => cu.Id == saleDto.CustomerId); if (!isCustomerIdExisting) { continue; } Sale newsale = new Sale() { CarId = saleDto.CarId, CustomerId = saleDto.CustomerId, Discount = saleDto.Discount }; salesToImport.Add(newsale); } context.Sales.AddRange(salesToImport); context.SaveChanges(); } return $"Successfully imported {salesToImport.Count}."; }