public void CreateOrUpdate(ReceptionBindingModel model) { using (var context = new SchoolAgainDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Reception element = model.Id.HasValue ? null : new Reception(); if (model.Id.HasValue) { element = context.Receptions.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } element.ClientId = model.ClientId; element.DateCreate = model.DateCreate; element.TotalSum = model.TotalSum; element.ReceptionStatus = model.ReceptionStatus; context.SaveChanges(); } else { element.ClientId = model.ClientId; element.DateCreate = model.DateCreate; element.TotalSum = model.TotalSum; element.ReceptionStatus = model.ReceptionStatus; context.Receptions.Add(element); context.SaveChanges(); var groupServices = model.ReceptionServices .GroupBy(rec => rec.ServiceId) .Select(rec => new { ServiceId = rec.Key, Count = rec.Sum(r => r.Count) }); foreach (var groupService in groupServices) { context.ReceptionServices.Add(new ReceptionService { ReceptionId = element.Id, ServiceId = groupService.ServiceId, Count = groupService.Count }); context.SaveChanges(); } } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void Delete(KidBindingModel model) { using (var context = new SchoolAgainDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Kid element = context.Kids.FirstOrDefault(rec => rec.Id == model.Id.Value); if (element != null) { context.ClientKids.RemoveRange( context.ClientKids.Where( rec => rec.KidId == model.Id.Value)); context.Kids.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void CreateOrUpdate(PaymentBindingModel model) { using (var context = new SchoolAgainDatabase()) { Payment element = model.Id.HasValue ? null : new Payment(); if (model.Id.HasValue) { element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Payment(); context.Payments.Add(element); } element.ReceptionId = model.ReceptionId; element.ClientId = model.ClientId; element.Sum = model.Sum; element.DatePayment = model.DatePayment; context.SaveChanges(); } }
public void CreateOrUpdate(ClientBindingModel model) { using (var context = new SchoolAgainDatabase()) { Client element = model.Id.HasValue ? null : new Client(); if (model.Id.HasValue) { element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Client(); context.Clients.Add(element); } element.ClientFIO = model.ClientFIO; element.Email = model.Email; element.Login = model.Login; element.Phone = model.Phone; element.Block = model.Block; element.Password = model.Password; context.SaveChanges(); } }
public void CreateOrUpdate(KidBindingModel model) { using (var context = new SchoolAgainDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Kid element = model.Id.HasValue ? null : new Kid(); if (model.Id.HasValue) { element = context.Kids.FirstOrDefault(rec => rec.ClientId == model.ClientId && rec.KidName == model.KidName); if (element == null) { throw new Exception("Такой питомец уже существует"); } element.KidName = model.KidName; element.Height = model.Height; element.Free = model.Free; element.Age = model.Age; element.Gender = model.Gender; element.ClientId = model.ClientId; context.SaveChanges(); } else { element.KidName = model.KidName; element.Height = model.Height; element.Free = model.Free; element.Age = model.Age; element.Gender = model.Gender; element.ClientId = model.ClientId; } context.Kids.Add(element); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void Delete(ReceptionBindingModel model) { using (var context = new SchoolAgainDatabase()) { Reception element = context.Receptions.FirstOrDefault(rec => rec.Id == model.Id.Value); if (element != null) { context.Receptions.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } } }
public void SaveToDatabase() { var services = LoadServices(); using (var context = new SchoolAgainDatabase()) { foreach (var service in services) { Service element = context.Services.FirstOrDefault(rec => rec.Id == service.Id); if (element != null) { break; } else { element = new Service(); context.Services.Add(element); } element.ServiceName = service.ServiceName; element.Price = service.Price; context.SaveChanges(); } } }