/// <summary> /// Static method to create new entity in the database /// </summary> /// <param name="_logEntry"></param> public static void CreateInDatabase(DriveLogEntry _logEntry) { using (DatabaseContext _context = new DatabaseContext()) { _context.DriveLogs.Add(_logEntry); _context.SaveChanges(); } }
public static void AddLogEntryFromUI(List <double> _inputArguments) { // Create a DriveLogEntry entity and adding it to the binding list DriveLogEntry driveLogEntry = new DriveLogEntry(_inputArguments[0], _inputArguments[1], _inputArguments[2], _inputArguments[3], _inputArguments[4]); BindingDriveLogEntriesList.Add(driveLogEntry); // Create new entity in DB Program.CreateInDatabase(driveLogEntry); }
public static void DeleteLogEntry(DriveLogEntry _logEntry) { // First check if the log entry is in the binding list if (BindingDriveLogEntriesList.Contains(_logEntry)) { BindingDriveLogEntriesList.Remove(_logEntry); } // Delete the entity in DB Program.DeleteFromDatabase(_logEntry); }
private void DataGrid_LogEntries_CellValueChanged(object sender, DataGridViewCellEventArgs e) { // Accessing the log entry that has been modified. e corresponds to the cell with the value changed try { DriveLogEntry changedLogEntry = DataManager.BindingDriveLogEntriesList[e.RowIndex]; // Updating the Database Program.UpdateDatabase(changedLogEntry); } catch (ArgumentOutOfRangeException e2) { return; } }
private static DriveLogEntry findEntityInDatabase(int _id) { using (DatabaseContext _context = new DatabaseContext()) { DriveLogEntry _entity = _context.DriveLogs.Find(_id); if (_entity == null) { MessageBox.Show($"Cannot find log entry with the ID '{_id}'!"); } return(_entity); } }
/// <summary> /// Static method to delete the selected entity from the database. /// </summary> /// <param name="_logEntry"></param> public static void DeleteFromDatabase(DriveLogEntry _logEntry) { using (DatabaseContext _context = new DatabaseContext()) { DriveLogEntry _entityLogEntry = findEntityInDatabase(_logEntry.ID); if (_entityLogEntry != null) { _context.Entry(_entityLogEntry).State = System.Data.Entity.EntityState.Deleted; _context.DriveLogs.Remove(_entityLogEntry); } else { MessageBox.Show($"Cannot delete the log entry with the ID '{_logEntry.ID}'!"); } _context.SaveChanges(); } }
/// <summary> /// Static method to update the database when any log entry has been modified. /// First receives the selected entity (log entry) and extracts the corresponding /// entity in the DbContext based on the ID. Each of the attributes are then updated /// and the DbContext is marked is "modified". Changes are then saved. /// </summary> /// <param name="_logEntry"></param> public static void UpdateDatabase(DriveLogEntry _logEntry) { using (DatabaseContext _context = new DatabaseContext()) { DriveLogEntry _entityLogEntry = findEntityInDatabase(_logEntry.ID); if (_entityLogEntry != null) { _entityLogEntry.Milage = _logEntry.Milage; _entityLogEntry.DistanceDriven = _logEntry.DistanceDriven; _entityLogEntry.FuelConsumed = _logEntry.FuelConsumed; _entityLogEntry.LKM = _logEntry.LKM; _entityLogEntry.MPG = _logEntry.MPG; // Mark as "modified" _context.Entry(_entityLogEntry).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); } } }
private void Delete_Btn_Click(object sender, EventArgs e) { // If user has not selectd any row, simply do nothing if (DataGrid_LogEntries.SelectedRows.Count == 0) { return; } // Else, delete the selected row (log entry) DriveLogEntry _logEntry = (DriveLogEntry)DataGrid_LogEntries.SelectedRows[0].DataBoundItem; // Simply do nothing if it's null if (_logEntry == null) { return; } else { DataManager.DeleteLogEntry(_logEntry); } }
public static void AddLogEntryFromDataBase(DriveLogEntry _logEntry) { BindingDriveLogEntriesList.Add(_logEntry); }