public void InsertOrUpdate(Schedule Schedule) { if (Schedule.Id == default(int)) { // New entity context.Schedules.Add(Schedule); } else { var InDb = context.Programs.Find(Schedule.Id); // Activity does not exist in database and it's new one if (InDb == null) { context.Schedules.Add(Schedule); return; } // Activity already exist in database and modify it context.Entry(InDb).CurrentValues.SetValues(Schedule); context.Entry(InDb).State = EntityState.Modified; } }
public void AddScheduleFromFile(Stream inputStream, string fileName) { IWorkbook _iWorkbook = null; if (fileName.Contains(".xlsx")) { _iWorkbook = new XSSFWorkbook(inputStream); } else if (fileName.Contains(".xls")) { _iWorkbook = new HSSFWorkbook(inputStream); } if (_iWorkbook != null) { string scheduleSign = ConfigurationManager.AppSettings["ScheduleSign"]; string showDaySign = ConfigurationManager.AppSettings["ShowDaySign"]; string headerRowKey = ConfigurationManager.AppSettings["HeaderRowKey"]; for (int i = 0; i < _iWorkbook.NumberOfSheets; i++) { ISheet sheet = _iWorkbook.GetSheetAt(i); if (_iHelper.CheckSchedule(sheet, scheduleSign)) { var listTime = _iHelper.GetTimeList(sheet, showDaySign); var startDay = listTime.FirstOrDefault(); var lastDay = listTime.LastOrDefault(); var startPoint = _iHelper.StartPoint(sheet, headerRowKey); _iCycleRepository.InsertOrUpdate(new Cycle() { Begin = startDay, End = lastDay }); _iCycleRepository.Save(); while (startDay <= lastDay) { // add data for (int j = startPoint.First() + 1; j <= sheet.LastRowNum; j++) { var row = sheet.GetRow(j); Schedule schedule = new Schedule(); if (row.GetCell(startPoint.Last()) != null && row.GetCell(startPoint.Last() + 4) != null) { schedule.ProgramCode = row.GetCell(startPoint.Last() + 4).StringCellValue.ToString(); if (schedule.ProgramCode != "") { var mytime = row.GetCell(startPoint.Last() + 1).DateCellValue; schedule.Date = new DateTime(startDay.Year, startDay.Month, startDay.Day, mytime.Hour, mytime.Minute, mytime.Second); _iScheduleRepository.InsertOrUpdate(schedule); } } } startDay = startDay.AddDays(1); } _iScheduleRepository.Save(); } } } }