protected override void OnNavigatedTo(NavigationEventArgs e) { if (e.Parameter is Vehicle) { SelectedPurchase = null; SelectedVehicle = e.Parameter as Vehicle; CommandLabel.Text = "Add New Fuel Purchase For " + SelectedVehicle.Name; } else { //SelectedPurchase = e.Parameter as Purchase; using (var db = new DbSchema()) { int id = (int)e.Parameter; SelectedPurchase = db.Purchases.Include(p => p.Vehicle).Single(p => p.ID == id); } SelectedVehicle = SelectedPurchase.Vehicle; Date.Date = SelectedPurchase.Date; Gallons.Text = SelectedPurchase.Gallons.ToString(); Cost.Text = SelectedPurchase.Cost.ToString(); Trip.Text = SelectedPurchase.Trip.ToString(); Odometer.Text = SelectedPurchase.Odometer.ToString(); CommandLabel.Text = "Edit Purchase For " + SelectedVehicle.Name + " On " + SelectedPurchase.Date.ToString(); } base.OnNavigatedTo(e); }
private List <Purchase> GetPurchasesForVehicle(Vehicle veh) { List <Purchase> purchList; using (var db = new DbSchema()) { purchList = db.Purchases.Where(p => p.Vehicle == veh).ToList(); } return(purchList); }
/// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public App() { this.InitializeComponent(); this.Suspending += OnSuspending; using (var db = new DbSchema()) { db.Database.Migrate(); } }
private void SaveButton_Click(object sender, RoutedEventArgs e) { using (var db = new DbSchema()) { if (SelectedPurchase == null) { DateTimeOffset tempDate = (DateTimeOffset)Date.Date; SelectedPurchase = new Purchase { Date = tempDate.Date, Gallons = Convert.ToDecimal(Gallons.Text), Cost = Convert.ToDecimal(Cost.Text), Trip = Convert.ToDecimal(Trip.Text), Odometer = Convert.ToInt32(Odometer.Text) }; // This is required but I don't know why. If I try to do // the Add directly on SelectedVehicle, the database doesn't // get modified at all. It is as if the Vehicle objects in the // list view are not the same as the Vehicle objects in the database; // something is missing that allows the database code to function // as expected. Vehicle tgt = db.Vehicles.Include(v => v.Purchases).Single(v => v.ID == SelectedVehicle.ID); // Vehicle tgt = db.Vehicles.Find(SelectedVehicle.ID); //SelectedPurchase.Vehicle = tgt; tgt.Purchases.Add(SelectedPurchase); //db.Purchases.Add(SelectedPurchase); } else { // Grab the info. DateTimeOffset tempDate = (DateTimeOffset)Date.Date; SelectedPurchase.Date = tempDate.Date; SelectedPurchase.Gallons = Convert.ToDecimal(Gallons.Text); SelectedPurchase.Cost = Convert.ToDecimal(Cost.Text); SelectedPurchase.Trip = Convert.ToDecimal(Trip.Text); SelectedPurchase.Odometer = Convert.ToInt32(Odometer.Text); // Mark the entry as having been modified. // Note that it is not worth trying to figure out if the // user really changed something or not. db.Entry(SelectedPurchase).State = EntityState.Modified; } db.SaveChanges(); } // Go back to the main page. this.Frame.GoBack(); }
private void DeletePurchase_Click(object sender, RoutedEventArgs e) { Purchase item = (Purchase)Purchases.SelectedItem; if (item != null) { using (var db = new DbSchema()) { db.Purchases.Remove(item); db.SaveChanges(); } Purchases.ItemsSource = GetPurchaseSummariesForVehicle(Vehicles.SelectedItem as Vehicle); } }
private void Delete_Click(object sender, RoutedEventArgs e) { Vehicle veh = Vehicles.SelectedItem as Vehicle; if (veh != null) { using (var db = new DbSchema()) { db.Vehicles.Remove(veh); db.SaveChanges(); Vehicles.ItemsSource = db.Vehicles.ToList(); } Vehicles.SelectedIndex = -1; } }
protected override void OnNavigatedTo(NavigationEventArgs e) { System.Diagnostics.Debug.WriteLine("Enter: OnNavigatedTo"); using (var db = new DbSchema()) { // Show list of all vehicles Vehicles.ItemsSource = db.Vehicles.ToList(); // Show purchases for selected vehicle, if there is one if (CurrentVehicleIndex != -1) { Vehicles.SelectedIndex = CurrentVehicleIndex; } } base.OnNavigatedTo(e); }
private void SaveButton_Click(object sender, RoutedEventArgs e) { using (var db = new DbSchema()) { if (SelectedVehicle == null) { SelectedVehicle = new Vehicle { Name = VehicleName.Text, Year = Convert.ToInt32(VehicleYear.Text), Make = VehicleMake.Text, Model = VehicleModel.Text, StartingMileage = Convert.ToInt32(StartingMileage.Text) }; db.Vehicles.Add(SelectedVehicle); } else { // Grab the info. SelectedVehicle.Name = VehicleName.Text; SelectedVehicle.Year = Convert.ToInt32(VehicleYear.Text); SelectedVehicle.Make = VehicleMake.Text; SelectedVehicle.Model = VehicleModel.Text; SelectedVehicle.StartingMileage = Convert.ToInt32(StartingMileage.Text); // Mark the entry as having been modified. // Note that it is not worth trying to figure out if the // user really changed something or not. db.Entry(SelectedVehicle).State = EntityState.Modified; } db.SaveChanges(); } // Go back to the main page. this.Frame.GoBack(); }
private void Vehicles_SelectionChanged(object sender, SelectionChangedEventArgs e) { System.Diagnostics.Debug.WriteLine("Enter: Vehicles_SelectionChanged"); CurrentVehicleIndex = Vehicles.SelectedIndex; if (CurrentVehicleIndex == -1) { HeaderText.Text = ""; NumPurchases.Text = ""; Purchases.ItemsSource = null; } else { using (var db = new DbSchema()) { NumPurchasesGlobal.Text = "Global purchases: " + db.Purchases.Count().ToString(); } Vehicle selVehicle = Vehicles.SelectedItem as Vehicle; HeaderText.Text = "Purchases For " + selVehicle.Name; NumPurchases.Text = "This vehicle has " + selVehicle.Purchases.Count.ToString() + " fuel purchases"; PurchasesVm.SetNewList(GetPurchaseSummariesForVehicle(selVehicle)); } }
private List <PurchaseSummary> GetPurchaseSummariesForVehicle(Vehicle veh) { List <Purchase> rawPurchases; using (var db = new DbSchema()) { rawPurchases = db.Purchases.Where(p => p.Vehicle == veh).ToList(); } List <PurchaseSummary> purchList = new List <PurchaseSummary>(); foreach (var rp in rawPurchases) { PurchaseSummary newPurch = new PurchaseSummary(); newPurch.Date = rp.Date.ToString("yyyy-MM-dd"); newPurch.Gallons = rp.Gallons.ToString("F3"); newPurch.Cost = rp.Cost.ToString("C"); newPurch.Mpg = Math.Round(rp.Trip / rp.Gallons, 1).ToString("F1"); newPurch.Dpg = Math.Round(rp.Cost / rp.Gallons, 2).ToString("C"); newPurch.Id = rp.ID; purchList.Add(newPurch); } return(purchList); }