public void Create(CustomerDto model) { var customerEntity = Mapper.Map <Customer>(model); foreach (var relationItem in customerEntity.Insurances) { var relation = new CustomerInsurances { CustomerId = customerEntity.Id, InsuranceId = relationItem.Id }; _insuranceCustomerRepository.Add(relation); } _customerRepository.Add(customerEntity); }
public void Update(CustomerDto model) { var customerEntity = Mapper.Map <Customer>(model); // Remove old references var insuraceReferences = _insuranceCustomerRepository.GetAllByCondition(x => x.CustomerId == model.Id); foreach (var insurance in insuraceReferences) { _insuranceCustomerRepository.Remove(insurance.Id); } // Add new references foreach (var relationItem in customerEntity.Insurances) { var relation = new CustomerInsurances { CustomerId = customerEntity.Id, InsuranceId = relationItem.Id }; _insuranceCustomerRepository.Add(relation); } _customerRepository.Update(customerEntity); }