public async Task Consume(ConsumeContext <FullOrgData> context) { //sort: department,position,employee var message = context.Message; if (message.Contacts == null || message.Contacts.Count == 0 || message.OrgUnits == null || message.OrgUnits.Count == 0) { return; } if (!await this.EnsureHistoryChangedAsync(message.DataVersion, message.OrgUnits.Count, message.Contacts.Count)) { return; } var departments = this._mapper.Map <List <Department> >(message.OrgUnits); var positions = departments.SelectMany(u => u.Positions).ToList(); var employees = this._mapper.Map <List <Employee> >(message.Contacts); var employeePositions = employees.SelectMany(u => u.Positions).ToList(); employeePositions = this.RemoveRepeatAndNull(employeePositions); using (var db = new ServiceDbContext(_dbOptions)) { try { await this.FillInManualDataAsync(db, departments, positions, employees, employeePositions); await db.BulkInsertOrUpdateOrDeleteAsync(departments); await db.BulkInsertOrUpdateOrDeleteAsync(positions); await db.BulkInsertOrUpdateOrDeleteAsync(employees); await db.BulkInsertOrUpdateOrDeleteAsync(employeePositions); await db.SaveChangesAsync(); await db.MdmDataHistories.AddAsync(new MdmDataHistory() { HistoryVersion = message.DataVersion, SyncTime = DateTimeOffset.Now, Description = $"Departments:{message.OrgUnits.Count},Employeees:{message.Contacts.Count}" }); await db.SaveChangesAsync(); } catch (Exception ex) { ServiceEventSource.Current.Message(ex.ToString()); } } }
public async Task Consume(ConsumeContext <FullOrgData> context) { //sort: department,position,employee var message = context.Message; if (message.Contacts == null || message.Contacts.Count == 0 || message.OrgUnits == null || message.OrgUnits.Count == 0) { return; } if (!await this.EnsureHistoryChangedAsync(message.DataVersion, message.OrgUnits.Count, message.Contacts.Count)) { return; } var departments = this._mapper.Map <List <Department> >(message.OrgUnits); var positions = departments.SelectMany(u => u.Positions).ToList(); var employees = this._mapper.Map <List <Employee> >(message.Contacts); var employeePositions = employees.SelectMany(u => u.Positions).ToList(); employeePositions = this.RemoveRepeatAndNull(employeePositions); using (var db = new ServiceDbContext(_dbOptions)) { try { await this.FillInManualDataAsync(db, departments, positions, employees, employeePositions); await db.BulkInsertOrUpdateOrDeleteAsync(departments); await db.BulkInsertOrUpdateOrDeleteAsync(positions); await db.BulkInsertOrUpdateOrDeleteAsync(employees); await db.BulkInsertOrUpdateOrDeleteAsync(employeePositions); await db.SaveChangesAsync(); await db.SaveChangesAsync(); } catch (Exception ex) { throw; } } }