Пример #1
0
        public async Task <IActionResult> DeleteConfirmed(Guid id)
        {
            var syncEmployeeController = new SyncEmployeeController(_context, syncConfiguration);
            var dependentEmployee      = await syncEmployeeController.GetDatas().Where(w => w.DepartmentID == id).FirstOrDefaultAsync();

            if (dependentEmployee != null)
            {
                throw new Exception($"The data is already used by Employee Name: {dependentEmployee.Name}");
            }

            var syncDepartment = await GetDatas().FirstAsync(m => m.ID == id);

            CustomSyncEngine customSyncEngine = new CustomSyncEngine(_context, syncConfiguration);

            customSyncEngine.HookPreDeleteGlobalTimeStamp(syncDepartment);

            _context.Update(syncDepartment);
            //_context.Departments.Remove(syncDepartment);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> DeleteConfirmed(Guid id)
        {
            var syncEmployeeController = new SyncEmployeeController(_context, syncConfiguration);
            var dependentEmployee      = await syncEmployeeController.GetDatas().Where(w => w.DepartmentID == id).FirstOrDefaultAsync();

            if (dependentEmployee != null)
            {
                throw new Exception($"The data is already used by Employee Name: {dependentEmployee.Name}");
            }

            var syncDepartment = await GetDatas().FirstAsync(m => m.ID == id);

            if (string.IsNullOrEmpty(syncDepartment.SynchronizationID))
            {
                throw new Exception("SynchronizationID cannot be empty");
            }

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    CustomSyncEngine customSyncEngine = new CustomSyncEngine(_context, syncConfiguration);
                    customSyncEngine.HookPreDeleteDatabaseTimeStamp(syncDepartment, transaction, syncDepartment.SynchronizationID, null);
                    _context.Update(syncDepartment);
                    //_context.Departments.Remove(syncDepartment);
                    await _context.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
            return(RedirectToAction(nameof(Index)));
        }