public int UpdateProductsCategory(int categoryID, ProductCategory newCategory) { Debug.Write(">>> Updating a products category ... "); DBStockCTX stockDB = new DBStockCTX(); /* * // 1st method: with a query * // init a query * var query = from prodCat in stockDB.productscategories * where prodCat.id == categoryID * select prodCat; * * // get the searched element * ProductCategory category = query.FirstOrDefault<ProductCategory>(); * * // 2nd method: without a query, use find method * ProductCategory prodCat = stockDB.productscategories.Find(categoryID); * * prodCat.name = newCategory.name; * prodCat.description = newCategory.description; * prodCat.products = newCategory.products; */ // 3rd method: using attach method stockDB.productscategories.Attach(newCategory); stockDB.Entry(newCategory).State = EntityState.Modified; stockDB.SaveChanges(); Debug.WriteLine("Done."); return(0); }
public int UpdateProduct(int productID, Product newProduct) { // PS: there is some exceptions to catch ie: null, ... Debug.Write(">>> Updating a new product ... "); try { DBStockCTX stockDB = new DBStockCTX(); /* * // 1st method: with a query * // init a query * var query = from prod in stockDB.products * where prod.id == productID * select prod; * * // get the searched element * Product product = query.FirstOrDefault<Product>(); * * // 2nd method: without a query, use find method * Product prod = stockDB.products.Find(productID); * prod.name = newProduct.name; * prod.description = newProduct.description; * prod.price = newProduct.price; * prod.category = newProduct.category; */ // 3rd method: using attach method stockDB.products.Attach(newProduct); stockDB.Entry(newProduct).State = EntityState.Modified; stockDB.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Error:"); Console.WriteLine(ex.Message); return(1); } Debug.WriteLine("Done."); return(0); }