private Tour CreateModel(TourBindingModel model, Tour tour, TourFirmDatabase context) { tour.Name = model.Name; tour.Country = model.Country; tour.Price = model.Price; tour.HaltID = model.HaltID; tour.OperatorID = model.OperatorID; if (model.ID.HasValue) { var tourGuides = context.TourGuides.Where(rec => rec.TourID == model.ID.Value).ToList(); // удалили те, которых нет в модели context.TourGuides.RemoveRange(tourGuides.ToList()); context.SaveChanges(); } // добавили новые foreach (var tg in model.TourGuides) { context.TourGuides.Add(new TourGuide { TourID = tour.ID, GuideID = tg.Key, }); context.SaveChanges(); } return(tour); }
public void Insert(TourBindingModel model) { using (var context = new TourFirmDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Tour tour = new Tour { Name = model.Name, Price = model.Price, Country = model.Country, HaltID = model.HaltID, OperatorID = model.OperatorID }; context.Tours.Add(tour); context.SaveChanges(); CreateModel(model, tour, context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
public void Insert(GuideBindingModel model) { using (var context = new TourFirmDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Guide guide = new Guide { Name = model.Name, Surname = model.Surname, PhoneNumber = model.PhoneNumber, WorkPlace = model.WorkPlace, MainLanguage = model.MainLanguage, AdditionalLanguage = model.AdditionalLanguage, OperatorID = model.OperatorID }; context.Guides.Add(guide); context.SaveChanges(); CreateModel(model, guide, context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
private Guide CreateModel(GuideBindingModel model, Guide guide, TourFirmDatabase context) { guide.Name = model.Name; guide.Surname = model.Surname; guide.PhoneNumber = model.PhoneNumber; guide.WorkPlace = model.WorkPlace; guide.MainLanguage = model.MainLanguage; guide.AdditionalLanguage = model.AdditionalLanguage; guide.OperatorID = model.OperatorID; if (model.GuideExcursions != null) { if (model.ID.HasValue) { var excursionGuides = context.GuideExcursions.Where(rec => rec.GuideID == model.ID.Value).ToList(); // удалили те, которых нет в модели context.GuideExcursions.RemoveRange(excursionGuides.Where(rec => !model.GuideExcursions.ContainsKey(rec.ExcursionID)).ToList()); context.SaveChanges(); } // добавили новые foreach (var eg in model.GuideExcursions) { context.GuideExcursions.Add(new GuideExcursion { GuideID = guide.ID, ExcursionID = eg.Key, }); context.SaveChanges(); } } return(guide); }
public void Update(TourBindingModel model) { using (var context = new TourFirmDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { var element = context.Tours.FirstOrDefault(rec => rec.ID == model.ID); if (element == null) { throw new Exception("Элемент не найден"); } CreateModel(model, element, context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
public void Insert(OperatorBindingModel model) { using (var context = new TourFirmDatabase()) { context.Operators.Add(CreateModel(model, new Operator())); context.SaveChanges(); } }
public void Insert(TouristBindingModel model) { using (var context = new TourFirmDatabase()) { context.Tourists.Add(CreateModel(model, new Tourist())); context.SaveChanges(); } }
public void Insert(ExcursionBindingModel model) { using (var context = new TourFirmDatabase()) { context.Excursions.Add(CreateModel(model, new Excursion())); context.SaveChanges(); } }
public void Update(OperatorBindingModel model) { using (var context = new TourFirmDatabase()) { var element = context.Operators.FirstOrDefault(rec => rec.ID == model.ID); if (element == null) { throw new Exception("Элемент не найден"); } CreateModel(model, element); context.SaveChanges(); } }
public void Delete(OperatorBindingModel model) { using (var context = new TourFirmDatabase()) { var element = context.Operators.FirstOrDefault(rec => rec.ID == model.ID); if (element != null) { context.Operators.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } } }
private Travel CreateModel(TravelBindingModel model, Travel travel, TourFirmDatabase context) { travel.Name = model.Name; travel.DateStart = model.DateStart; travel.DateEnd = model.DateEnd; travel.TouristID = model.TouristID; if (travel.ID == 0) { context.Travels.Add(travel); context.SaveChanges(); } if (model.ID.HasValue) { List <TravelTour> travelTours = context.TravelTours .Where(rec => rec.TravelID == model.ID.Value).ToList(); context.TravelTours.RemoveRange(travelTours .Where(rec => !model.TravelTours.ContainsKey(rec.TourID)).ToList()); List <TravelExcursion> travelExcursions = context.TravelExcursions .Where(rec => rec.TravelID == model.ID.Value).ToList(); context.TravelExcursions.RemoveRange(travelExcursions .Where(rec => !model.TravelExcursions.ContainsKey(rec.ExcursionID)).ToList()); context.SaveChanges(); // Убираем повторы foreach (var travelTour in travelTours) { if (model.TravelTours.ContainsKey(travelTour.TourID)) { model.TravelTours.Remove(travelTour.TourID); } } foreach (var travelExcursion in travelExcursions) { if (model.TravelExcursions.ContainsKey(travelExcursion.ExcursionID)) { model.TravelExcursions.Remove(travelExcursion.ExcursionID); } } context.SaveChanges(); } foreach (var tt in model.TravelTours) { context.TravelTours.Add(new TravelTour { TravelID = travel.ID, TourID = tt.Key, }); context.SaveChanges(); } foreach (var te in model.TravelExcursions) { context.TravelExcursions.Add(new TravelExcursion { TravelID = travel.ID, ExcursionID = te.Key, }); context.SaveChanges(); } return(travel); }