public async Task OnGetAsync(int?id, int?drinkID) { CompanyData = new CompanyIndexData(); // Eager Loading of Company Data CompanyData.Companies = await _context.Companies .Include(i => i.CompanyHQ) .Include(i => i.DrinkAssignments) .ThenInclude(i => i.Drink) .ThenInclude(i => i.DrinkCategory) /*.Include(i => i.DrinkAssignments) * .ThenInclude(i => i.Drink) * .ThenInclude(i => i.Menus) * .ThenInclude(i => i.Food) * .AsNoTracking()*/ .OrderBy(i => i.Name) .ToListAsync(); if (id != null) { CompanyID = id.Value; Company company = CompanyData.Companies .Where(i => i.CompanyID == id.Value).Single(); CompanyData.Drinks = company.DrinkAssignments.Select(s => s.Drink); } if (drinkID != null) { DrinkID = drinkID.Value; /*CompanyData.Menus = CompanyData.Drinks.Single( * x => x.DrinkID == drinkID).Menus;*/ var selectedDrink = CompanyData.Drinks .Where(x => x.DrinkID == drinkID).Single(); //Explicit Loading of Menu Data await _context.Entry(selectedDrink).Collection(x => x.Menus).LoadAsync(); foreach (Menu menu in selectedDrink.Menus) { await _context.Entry(menu).Reference(x => x.Food).LoadAsync(); } //End of Explicit Loading CompanyData.Menus = selectedDrink.Menus; } /*Company = await _context.Companies.ToListAsync();*/ }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var drinkCategoryToUpdate = await _context.DrinkCategories .Include(i => i.CompanyHead) .FirstOrDefaultAsync(m => m.DrinkCategoryID == id); if (drinkCategoryToUpdate == null) { return(HandleDeletedDrinkCategory()); } _context.Entry(drinkCategoryToUpdate) .Property("RowVersion") .OriginalValue = DrinkCategory.RowVersion; if (await TryUpdateModelAsync <DrinkCategory>( drinkCategoryToUpdate, "Drink Category", s => s.Name, s => s.Alcoholic, s => s.MinProductionCost, s => s.CompanyID)) { try { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var clientValues = (DrinkCategory)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save. " + "The department was deleted by another user."); return(Page()); } var dbValues = (DrinkCategory)databaseEntry.ToObject(); await setDbErrorMessage(dbValues, clientValues, _context); //Save the current RowVersion so next postback matched unless an new concurrency issue happens. DrinkCategory.RowVersion = (byte[])dbValues.RowVersion; //Clear the model error for the next postback. ModelState.Remove("Department.RowVersion"); } } CompanyNameSL = new SelectList(_context.Companies, "ID", "Name", drinkCategoryToUpdate.CompanyID); return(Page()); /*_context.Attach(DrinkCategory).State = EntityState.Modified; // before Concurrency implementation * * try * { * await _context.SaveChangesAsync(); * } * catch (DbUpdateConcurrencyException) * { * if (!DrinkCategoryExists(DrinkCategory.DrinkCategoryID)) * { * return NotFound(); * } * else * { * throw; * } * } * * return RedirectToPage("./Index");*/ }