public void AddProduct(Product product) { InventoryDB.Inventory.InsertOnSubmit(product); /* Add to data context */ InventoryDB.SubmitChanges(); /* Save changes to the DB */ Products.Add(product); /* Add to observable collection */ }
public void PopulateProductDB() { /* I'm having an error right now where each time I run the application I'm putting all of these Products in the database again... :( */ /* Start by creating a list of Products. We'll actually insert them into the database later */ ProductEqualityComparer pc = new ProductEqualityComparer(); List <Product> ProductInventory = new List <Product>(); if (!App.GlobalVars.DBHasBeenPopulated) { #region Hats ProductInventory.Add(new Product() { title = "Black Hat", imageUrl = "/Images/BlackHat.jpg", subcategory = "Hats", price = 19.99, quantity = 30 }); ProductInventory.Add(new Product() { title = "Gray Hat", imageUrl = "/Images/GrayHat.jpg", subcategory = "Hats", price = 19.99, quantity = 30 }); ProductInventory.Add(new Product() { title = "Crimson Hat", imageUrl = "/Images/Hats.jpg", subcategory = "Hats", price = 19.99, quantity = 40 }); ProductInventory.Add(new Product() { title = "Beanie", imageUrl = "/Images/Men_Beanie.jpg", subcategory = "Hats", price = 14.99, quantity = 50 }); ProductInventory.Add(new Product() { title = "Tie Up Beanie", imageUrl = "/Images/TieUpBeanie.jpg", subcategory = "Hats", price = 14.99, quantity = 50 }); ProductInventory.Add(new Product() { title = "Visor", imageUrl = "/Images/Visor.jpg", subcategory = "Hats", price = 10, quantity = 1 }); #endregion #region Men ProductInventory.Add(new Product() { title = "Black Polo", imageUrl = "/Images/BlackPolo.jpg", subcategory = "Men", price = 39.99, quantity = 25 }); ProductInventory.Add(new Product() { title = "Jersey", imageUrl = "/Images/Men_Jersey.jpg", subcategory = "Men", price = 65.99, quantity = 50 }); ProductInventory.Add(new Product() { title = "T-Shirt", imageUrl = "/Images/Men_Tshirt.jpg", subcategory = "Men", price = 14.99, quantity = 50 }); #endregion #region Women ProductInventory.Add(new Product() { title = "V-neck", imageUrl = "/Images/Women.jpg", subcategory = "Women", price = 14.99, quantity = 1 }); ProductInventory.Add(new Product() { title = "Hooded Sweatshirt", imageUrl = "/Images/Women_Hoodie.jpg", subcategory = "Women", price = 14.99, quantity = 50 }); ProductInventory.Add(new Product() { title = "Jersey", imageUrl = "/Images/Women_Jersey.jpg", subcategory = "Women", price = 14.99, quantity = 50 }); ProductInventory.Add(new Product() { title = "Long Sleeve Tee", imageUrl = "/Images/Women_LongSleeve.jpg", subcategory = "Women", price = 14.99, quantity = 50 }); ProductInventory.Add(new Product() { title = "Women's Athletic Pants", imageUrl = "/Images/Women_PerformancePants.jpg", subcategory = "Women", price = 24.99, quantity = 50 }); ProductInventory.Add(new Product() { title = "V-neck", imageUrl = "/Images/Women_VNeck.jpg", subcategory = "Women", price = 14.99, quantity = 50 }); #endregion #region Performance Apparel ProductInventory.Add(new Product() { title = "Black Golf Shirt", imageUrl = "/Images/BlackPolo.jpg", subcategory = "PerformanceApparel", price = 39.99, quantity = 25 }); ProductInventory.Add(new Product() { title = "Athletic Shorts", imageUrl = "/Images/Men_Shorts.jpg", subcategory = "PerformanceApparel", price = 29.99, quantity = 30 }); ProductInventory.Add(new Product() { title = "Athletic Shorts", imageUrl = "/Images/PerformanceApparel.jpg", subcategory = "PerformanceApparel", price = 29.99, quantity = 30 }); ProductInventory.Add(new Product() { title = "Women's Athletic Pants", imageUrl = "/Images/Women_PerformancePants.jpg", subcategory = "PerformanceApparel", price = 24.99, quantity = 50 }); #endregion /* Add the products to the database */ foreach (Product product in ProductInventory) { if (!InventoryDB.Inventory.Contains <Product>(product)) { AddProduct(product); } } InventoryDB.SubmitChanges(); } App.GlobalVars.DBHasBeenPopulated = true; }