public void UpdateShippingCosts(UpdateShippingCostsRequest data) { using (var db = new Entities()) { foreach (var country in data.Countries) { foreach (var cost in country.ShippingCosts) { var shippingCost = db.ShippingCosts.SingleOrDefault(sc => sc.CountryId == country.Id && sc.ShippingCategoryId == cost.ShippingCategoryId); if (shippingCost != null) { shippingCost.Amount = cost.Amount; shippingCost.AdditionalAmount = cost.AdditionalAmount; db.Entry(shippingCost).State = EntityState.Modified; } else { shippingCost = new ShippingCost { CountryId = country.Id, ShippingCategoryId = cost.ShippingCategoryId, Amount = cost.Amount, AdditionalAmount = cost.AdditionalAmount }; db.ShippingCosts.Add(shippingCost); } } } db.SaveChanges(); } }
private void setShippingCosts(Entities db, string countryId, int shippingCategoryId, decimal amount, decimal additionalAmount) { var costs = db.ShippingCosts.SingleOrDefault(sc => sc.CountryId == countryId && sc.ShippingCategoryId == shippingCategoryId); if (costs == null) { costs = new ShippingCost { CountryId = countryId, ShippingCategoryId = shippingCategoryId }; } costs.Amount = amount; costs.AdditionalAmount = additionalAmount; db.SaveChanges(); }