public static async Task ResetStockTypeQuantity() { DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>(); optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true"); using var context = new ManufacturingContext(optionsBuilder.Options); var tma = await context.Categories.OfType <StockType>().Include(e => e.PartInstances).ThenInclude(e => e.BubblerParameter).FirstOrDefaultAsync(e => e.Id == 17); foreach (var instance in tma.PartInstances) { Console.WriteLine("PartInstance: {0} Quantity", instance.BubblerParameter.Weight); } tma.Quantity = 0; tma.Quantity += (int)tma.PartInstances.Sum(instance => instance.BubblerParameter.Weight); Console.WriteLine(); Console.WriteLine("New Quantity: {0}", tma.Quantity); context.Update(tma); await context.SaveChangesAsync(); Console.WriteLine("Should be done"); }
public static void TestingStockTypes() { DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>(); optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true"); using var context = new ManufacturingContext(optionsBuilder.Options); Console.WriteLine("Updating Stock Type"); var stockType = context.Categories.OfType <StockType>().Include(e => e.PartInstances).ThenInclude(e => e.BubblerParameter).FirstOrDefault(e => e.Name == "TMA"); if (stockType != null) { stockType.Quantity += (int)stockType.PartInstances.Sum(e => e.BubblerParameter.Weight); context.Update(stockType); Console.WriteLine("New Quantity: {0}", stockType.Quantity); Console.WriteLine("Save Changes..."); context.SaveChanges(); } else { Console.WriteLine("StockType Not Found"); } Console.WriteLine("Exiting"); }
public static async Task ChangeStockTypeThenAlert() { DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>(); optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true"); using var context = new ManufacturingContext(optionsBuilder.Options); var user = context.Users .Include(e => e.Sessions) .ThenInclude(e => e.Transactions) .Include(e => e.Permission) .FirstOrDefault(e => e.FirstName == "Andrew"); UserService userService = new UserService(); if (user != null) { Session session = new Session(user); context.Sessions.Add(session); context.SaveChanges(); userService.CurrentUserName = user.UserName; userService.CurrentSessionId = session.Id; userService.UserPermissionName = user.Permission.Name; } var partInstance = await context.PartInstances.Include(e => e.IndividualAlert).Include(e => e.StockType).ThenInclude(e => e.CombinedAlert).FirstOrDefaultAsync(e => e.Id == 66); var newStockType = await context.Categories.OfType <StockType>().Include(e => e.CombinedAlert).ThenInclude(e => e.UserAlerts).FirstOrDefaultAsync(e => e.Id == 14); if (partInstance != null && newStockType != null) { if (newStockType.IsDefault) { if (!partInstance.StockType.IsDefault) { //from combinded to individual Console.WriteLine("Combined to Individual"); IndividualAlert alert = new IndividualAlert(); alert.PartInstance = partInstance; partInstance.IndividualAlert = alert; context.Add(alert); partInstance.StockType = newStockType; context.Update(partInstance); await context.SaveChangesAsync(); Console.WriteLine("Case one should be done"); } else { //from individual to individual. Should never be here Console.WriteLine("You should not be here"); } } else { if (partInstance.StockType.IsDefault) { if (partInstance.IndividualAlert != null) { //from individual to combined var userAlerts = context.UserAlerts.Where(e => e.AlertId == partInstance.IndividualAlertId); context.RemoveRange(userAlerts); var deleted = context.Alerts.Remove(partInstance.IndividualAlert); partInstance.IndividualAlert = null; partInstance.StockType = newStockType; newStockType.Quantity += partInstance.Quantity; context.Update(newStockType); await context.SaveChangesAsync(); Console.WriteLine("Should be done"); } else { Console.WriteLine("You should not be here"); } } else { //from combined to another combined Console.WriteLine("Combined to Combined"); var oldStock = context.Entry <StockType>(partInstance.StockType).Entity; oldStock.Quantity -= partInstance.Quantity; var okay = oldStock.PartInstances.Remove(partInstance); partInstance.StockType = newStockType; newStockType.PartInstances.Add(partInstance); newStockType.Quantity += partInstance.Quantity; context.Update(newStockType); context.Update(oldStock); context.Update(partInstance); await context.SaveChangesAsync(); Console.WriteLine("Should be finished"); } } Console.WriteLine("Done, Press any key to exit"); } else { Console.WriteLine("PartInstance not found"); } }