private Request CreateModel(RequestBindingModel model, Request request, CTODatabase context) { request.ClientId = model.ClientId; request.RequestName = model.RequestName; request.RequestCost = model.RequestCost; request.DateFrom = model.DateFrom; request.DateTo = model.DateTo; if (model.Id.HasValue) { var requestWorks = context.RequestWorks.Where(rec => rec.RequestId == model.Id.Value).ToList(); context.RequestWorks.RemoveRange(requestWorks.Where(rec => !model.RequestWorks.ContainsKey(rec.WorkId)).ToList()); context.SaveChanges(); foreach (var updateComponent in requestWorks) { model.RequestWorks.Remove(updateComponent.WorkId); } context.SaveChanges(); } foreach (var pc in model.RequestWorks) { context.RequestWorks.Add(new RequestWork { RequestId = request.Id, WorkId = pc.Key, }); context.SaveChanges(); } return(request); }
public void Update(RequestBindingModel model) { using (var context = new CTODatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { var element = context.Requests.Include(rec => rec.RequestWorks) .ThenInclude(rec => rec.Work).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(WorkBindingModel model) { using (var context = new CTODatabase()) { context.Works.Add(CreateModel(model, new Work())); context.SaveChanges(); } }
public void Insert(CostBindingModel model) { using (var context = new CTODatabase()) { context.Costs.Add(CreateModel(model, new Cost())); context.SaveChanges(); } }
public void Update(WorkBindingModel model) { using (var context = new CTODatabase()) { var element = context.Works.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Такого ремонта нет"); } CreateModel(model, element); context.SaveChanges(); } }
public void Update(CostBindingModel model) { using (var context = new CTODatabase()) { var element = context.Costs.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Статья затрат не найдена"); } CreateModel(model, element); context.SaveChanges(); } }
public void Update(WorkerBindingModel model) { using (var context = new CTODatabase()) { var element = context.Workers.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Сотрудник не найден"); } CreateModel(model, element); context.SaveChanges(); } }
public void Delete(WorkBindingModel model) { using (var context = new CTODatabase()) { Work element = context.Works.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Works.Remove(element); context.SaveChanges(); } else { throw new Exception("Такого ремонта нет"); } } }
public void Delete(CostBindingModel model) { using (var context = new CTODatabase()) { Cost element = context.Costs.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Costs.Remove(element); context.SaveChanges(); } else { throw new Exception("Статья затрат не найдена"); } } }
public void Delete(WorkerBindingModel model) { using (var context = new CTODatabase()) { Worker element = context.Workers.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Workers.Remove(element); context.SaveChanges(); } else { throw new Exception("Сотрудник не найден"); } } }
public void Delete(RequestBindingModel model) { using (var context = new CTODatabase()) { Request element = context.Requests.Include(rec => rec.RequestWorks) .ThenInclude(rec => rec.Work).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Requests.Remove(element); context.SaveChanges(); } else { throw new Exception("Заявка не найдена"); } } }
public void Insert(RequestBindingModel model) { using (var context = new CTODatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Request request = CreateModel(model, new Request()); context.Requests.Add(request); context.SaveChanges(); CreateModel(model, request, context); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }