public SaveMirDataTableResponse SaveMirDataTableRespons(SaveMirDataTableRequest request) { var MirDataTable = request.MapTo<MirDataTable>(); if (request.MirDataId == 0) { MirDataTable.MirConfiguration = DataContext.MirConfigurations.FirstOrDefault(x => x.Id == request.MirConfigurationId); DataContext.MirDataTables.Add(MirDataTable); DataContext.SaveChanges(); //MirDataTable.Kpis = new List<Kpi>(); foreach(var kpiId in request.KpiIds) { var kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpiId); if (kpi == null ) { kpi = new Kpi { Id = kpiId }; DataContext.Kpis.Attach(kpi); } DataContext.MirTableKpis.Add(new MirTableKpi { Kpi = kpi, MirTable = MirDataTable }); //MirDataTable.Kpis.Add(kpi); } } else { MirDataTable = DataContext.MirDataTables.Include(x => x.Kpis).FirstOrDefault(x => x.Id == request.MirDataId); request.MapPropertiesToInstance<MirDataTable>(MirDataTable); foreach (var mirTableKpi in MirDataTable.Kpis.ToList()) { MirDataTable.Kpis.Remove(mirTableKpi); } //MirDataTable.Kpis = new List<Kpi>(); foreach(var kpiId in request.KpiIds) { var kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpiId); if(kpi == null) { kpi = new Kpi { Id = kpiId }; DataContext.Kpis.Attach(kpi); } DataContext.MirTableKpis.Add(new MirTableKpi { Kpi = kpi, MirTable = MirDataTable }); //MirDataTable.Kpis.Add(kpi); } } DataContext.SaveChanges(); return new SaveMirDataTableResponse { KpiIds = MirDataTable.Kpis.Select(x => x.Id).ToArray(), IsSuccess = true, Message = "Mir Data Table has been saved succesfully" }; }
public SaveMirDataTableResponse SaveMirDataTableRespons(SaveMirDataTableRequest request) { var MirDataTable = request.MapTo<MirDataTable>(); if (request.MirDataId == 0) { MirDataTable.MirConfiguration = DataContext.MirConfigurations.FirstOrDefault(x => x.Id == request.MirConfigurationId); MirDataTable.Kpis = new List<Kpi>(); foreach(var kpiId in request.KpiIds) { var kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpiId); if (kpi == null ) { kpi = new Kpi { Id = kpiId }; DataContext.Kpis.Attach(kpi); } MirDataTable.Kpis.Add(kpi); } DataContext.MirDataTables.Add(MirDataTable); } else { MirDataTable = DataContext.MirDataTables.FirstOrDefault(x => x.Id == request.MirDataId); request.MapPropertiesToInstance<MirDataTable>(MirDataTable); MirDataTable.Kpis = new List<Kpi>(); foreach(var kpiId in request.KpiIds) { var kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpiId); if(kpi == null) { kpi = new Kpi { Id = kpiId }; DataContext.Kpis.Attach(kpi); } MirDataTable.Kpis.Add(kpi); } } DataContext.SaveChanges(); return new SaveMirDataTableResponse { IsSuccess = true, Message = "Mir Data Table has been saved succesfully" }; }