private void removeRecords() { if (curTableName == "TICKETS" || curTableName == "LOGTABLE" || curTableName == "LOGGINGACTIONS") { return; } RailwaysEntities context = RailwaysData.sharedContext; object entities = context.GetType().GetProperty(curTableName).GetValue(context, null); MethodInfo mListDelete = entities.GetType().GetMethod("DeleteObject"); for (int i = DataTab.SelectedItems.Count - 1; i >= 0; --i) { object row = DataTab.SelectedItems[i]; try { mListDelete.Invoke(entities, new object[] { row }); } catch { }; } try { context.SaveChanges(); LoggingManager.LogAction(4, curTableName); } catch (Exception ex) { /*if (ex.InnerException == null) * MessageBox.Show(ex.Message); * else * MessageBox.Show(ex.InnerException.Message);*/ } }
private void DataTab_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { RailwaysEntities context = RailwaysData.sharedContext; bool adding = false; try { object entities = context.GetType().GetProperty(curTableName).GetValue(context, null); object dataContext = e.Row.DataContext; try{ object obj = dataContext.GetType().GetProperty("ID").GetValue(dataContext, null); int isNewId; if (obj.GetType() == typeof(short)) { isNewId = (short)obj; if (isNewId == 0) { dataContext.GetType().GetProperty("ID").SetValue(dataContext, (short)RailwaysData.GetIndexForTable(curTableName), null); } } else { isNewId = (int)obj; //0 - значение id вместо null. Все валиные id в базе > 0 if (isNewId == 0) { dataContext.GetType().GetProperty("ID").SetValue(dataContext, RailwaysData.GetIndexForTable(curTableName), null); } } } catch {} MethodInfo mListAdd = entities.GetType().GetMethod("AddObject"); mListAdd.Invoke(entities, new object[] { dataContext }); adding = true; } catch { } try { context.SaveChanges(); context.AcceptAllChanges(); LoggingManager.LogAction(adding ? 6 : 5, curTableName); } catch (Exception ex) { if (ex.InnerException == null) { MessageBox.Show(ex.Message); } else { MessageBox.Show(ex.InnerException.Message); } } }
public static List <ProfitRecord> GetProfits() { RailwaysEntities context = RailwaysData.sharedContext; Dictionary <int, ProfitRecord> groups = new Dictionary <int, ProfitRecord>(); List <TICKET> tickets = context.TICKETS.ToList(); foreach (TICKET ticket in tickets) { int?id = ticket.RACE_ID; if (!groups.ContainsKey((int)id)) { ProfitRecord record = new ProfitRecord(); TrainDestinations dest = GetOneTrainDestination((int)ticket.RACE.TRAIN_ID); record.raceID = (int)id; record.trainID = (int)dest.ID; record.start = dest.start; record.finish = dest.finish; if (ticket.RACE.FORWARD == 1) { string x = record.start; record.start = record.finish; record.finish = x; } record.ticketsSold = 0; record.profit = 0; groups.Add((int)id, record); } ProfitRecord profRec = groups[(int)ticket.RACE_ID]; profRec.profit += (float)ticket.PRICE; profRec.ticketsSold++; context.SaveChanges(); } List <ProfitRecord> res = groups.Values.ToList(); res.Sort(profitRecordComparer); return(res); }