private int AddProductSchedultShows(DateTime[] selectedDates, ProductScheduleShow originModel) { int result = CRUDStatusCode.ERROR; try { _productScheduleShowService = new ProductScheduleShowService(); using (var context = new ATVEntities()) { using (DbContextTransaction transaction = context.Database.BeginTransaction()) { try { var pssInDB = context.ProductScheduleShows.Where(p => p.ContractDetailId == model.ContractDetailId && p.TimeSlot == model.TimeSlot && p.TimeSlotCode == model.TimeSlotCode && p.TimeSlotLength == model.TimeSlotLength) .ToList(); if (pssInDB != null) { foreach (var item in pssInDB) { context.ProductScheduleShows.Remove(item); context.SaveChanges(); } } ProductScheduleShow pss = null; foreach (var date in selectedDates) { pss = originModel; pss.ShowDate = date; context.ProductScheduleShows.Add(pss); context.SaveChanges(); } transaction.Commit(); result = CRUDStatusCode.SUCCESS; } catch (Exception ex) { transaction.Rollback(); throw; } } } } catch (Exception ex) { throw; } finally { _productScheduleShowService = null; } return(result); }
private void btnCancel_Click2(object sender, EventArgs e) { if (Utilities.ShowConfirmMessage(CommonMessage.CONFIRM_CANCLE)) { int result = CRUDStatusCode.ERROR; using (var context = new ATVEntities()) { using (DbContextTransaction transaction = context.Database.BeginTransaction()) { try { var contract = context.Contracts.FirstOrDefault(c => c.Id == model.Id); if (contract != null) { contract.StatusId = Constants.CommonStatus.CANCEL; context.SaveChanges(); var contractItems = context.ContractItems.Where(ci => ci.ContractCode == contract.Code); foreach (var ci in contractItems) { ci.StatusId = Constants.CommonStatus.CANCEL; var producSchedulShowes = context.ProductScheduleShows.Where(p => p.ContractDetailId == ci.Id); context.ProductScheduleShows.RemoveRange(producSchedulShowes); context.SaveChanges(); } } transaction.Commit(); result = CRUDStatusCode.SUCCESS; Utilities.ShowMessage(CommonMessage.CANCEL_SUCESSFULLY); Logging.LogBusiness(string.Format("{0} {1} {2}", Common.Session.GetUserName(), Common.Constants.LogAction.Cancle, "hợp đồng mã " + model.Code), Common.Constants.BusinessLogType.Delete); UpdateContractCost(); gbContractDetail.Visible = false; } catch (Exception ex) { gbContractDetail.Visible = true; transaction.Rollback(); throw ex; } } } } }
private void btnDeleteDetail_Click2(object sender, EventArgs e) { if (Utilities.ShowConfirmMessage(CommonMessage.CONFIRM_DELETE)) { using (var context = new ATVEntities()) { using (DbContextTransaction transaction = context.Database.BeginTransaction()) { try { if (contractDetail != null) { var ctrDetail = context.ContractItems.FirstOrDefault(c => c.Id == contractDetail.Id); if (ctrDetail != null) { ctrDetail.StatusId = Constants.CommonStatus.CANCEL; var producSchedulShowes = context.ProductScheduleShows.Where(p => p.ContractDetailId == ctrDetail.Id); if (producSchedulShowes.Count() > 0) { context.ProductScheduleShows.RemoveRange(producSchedulShowes); } context.SaveChanges(); } } transaction.Commit(); UpdateContractCost(); LoadDGV(); Utilities.ShowMessage(CommonMessage.DELETE_SUCESSFULLY); Logging.LogBusiness(string.Format("{0} {1} {2}", Common.Session.GetUserName(), Common.Constants.LogAction.Delete, "hợp đồng mã " + model.Code), Common.Constants.BusinessLogType.Delete); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } } }
public Repository() { this.context = new ATVEntities(); this.dbSet = context.Set <TEntity>(); }