public static void addCacheOnly(Tb_taskModelMap taskModelMap) { try { list.Add(taskModelMap); } catch (Exception ex) { MessageBoxEx.Show("缓存任务过程时出现异常:" + ex); } }
/// <summary> /// 新增任务,及任务相关的表(任务指标映射表、任务生命周期表) /// </summary> /// <param name="task"></param> /// <param name="indicatorIdList"></param> public static void createTask(Tb_taskInfo task, List <int> indicatorIdList, Dictionary <int, List <int> > brandModelIdDic) { using (EFMySqlDbContext context = new Db.EFMySqlDbContext()) { using (var transaction = context.Database.BeginTransaction()) { try { //新增任务信息 context.Tb_taskInfos.Add(task); context.SaveChanges(); context.Entry(task); int taskId = task.id; //添加任务生命周期 Tb_taskLifecycle lifecycle = new Tb_taskLifecycle(); lifecycle.taskId = taskId; lifecycle.taskStateDateTime = DateTime.Now; lifecycle.taskStateChangeExecutor = User.currentUser.name; lifecycle.taskState = (int)TaskStateEnum.Created; lifecycle.remark = string.Empty; context.Tb_taskLifecycles.Add(lifecycle); context.SaveChanges(); //添加任务型号映射 List <Tb_taskModelMap> tmmList = new List <Entity.Tb_taskModelMap>(); foreach (int brandId in brandModelIdDic.Keys) { foreach (int modelId in brandModelIdDic[brandId]) { Tb_taskModelMap tmm = new Tb_taskModelMap(); tmm.taskId = taskId; tmm.brandId = brandId; tmm.ModelId = modelId; tmmList.Add(tmm); } } context.Tb_taskModelMaps.AddRange(tmmList); context.SaveChanges(); //添加任务指标映射 List <Tb_taskIndicatorMap> timList = new List <Tb_taskIndicatorMap>(); foreach (int indicatorId in indicatorIdList) { Tb_taskIndicatorMap tim = new Tb_taskIndicatorMap(); tim.taskId = taskId; tim.indicatorId = indicatorId; timList.Add(tim); } context.Tb_taskIndicatorMaps.AddRange(timList); context.SaveChanges(); //提交数据库 transaction.Commit(); //更新任务生命周期缓存 TaskLifecycleCache.addCacheOnly(lifecycle); //更新品牌型号映射缓存 foreach (var tmm in tmmList) { TaskModelMapCache.addCacheOnly(tmm); } //更新指标映射缓存 foreach (var tim in timList) { TaskIndicatorMapCache.addCacheOnly(tim); } } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }