// This method updates a Category record in the Warehouse database and returns true if the operation was successful.
        public bool Update_Category_By_ID(DataModel.Category category)
        {
            using (var context = new DataModel.WarehouseContext())
            {
                // Get and update Category
                bool success = false;
                var  cat     = context.Categories.SingleOrDefault(c => c.Category_ID == category.Category_ID);
                if (cat != null)
                {
                    cat.Category_Name        = category.Category_Name;
                    cat.Category_Description = category.Category_Description;

                    //Apply change
                    context.Categories.Attach(cat);
                    context.Entry(cat).State = System.Data.Entity.EntityState.Modified;
                }

                //Check execution of transaction - we expect 1 change to have occurred
                var execution_result = context.SaveChanges();
                if (execution_result != 1)
                {
                    success = false;
                }

                return(success);
            }
        }
Exemplo n.º 2
0
        //Given a Product object, update the Products table on Product_ID
        //Returns a boolean
        public bool Update_Product_By_Product_ID(DataModel.Product product_in)
        {
            var ret = true;

            using (var context = new DataModel.WarehouseContext())
            {
                // Get and update Product
                var product = context.Products.SingleOrDefault(p => p.Product_ID == product_in.Product_ID);
                if (product != null)
                {
                    product.Product_Name  = product_in.Product_Name;
                    product.Product_Price = product_in.Product_Price;
                    product.Product_UPC   = product_in.Product_UPC;
                    product.CategoryRefID = product_in.CategoryRefID;

                    //Apply change
                    context.Products.Attach(product);
                    context.Entry(product).State = System.Data.Entity.EntityState.Modified;

                    //Check execution of transaction - we expect 1 change to have occurred
                    var execution_result = context.SaveChanges();
                    if (execution_result != 1)
                    {
                        ret = false;
                    }
                }
                else
                {
                    ret = false;
                }
            }

            return(ret);
        }
        //Given a Warehouse object, update the Warehouses table on Warehouse_ID
        //Returns a boolean
        public bool Update(DataModel.Warehouse warehouse_in, ref string msg)
        {
            var ret = true;

            using (var context = new DataModel.WarehouseContext())
            {
                // Get and update Warehouse
                var warehouse = context.Warehouses.First(w => w.Warehouse_ID == warehouse_in.Warehouse_ID);
                if (warehouse != null)
                {
                    warehouse.Warehouse_Name = warehouse_in.Warehouse_Name;
                    warehouse.Street         = warehouse_in.Street;
                    warehouse.City           = warehouse_in.City;
                    warehouse.State          = warehouse_in.State;
                    warehouse.Zipcode        = warehouse_in.Zipcode;

                    //Apply change
                    context.Warehouses.Attach(warehouse);
                    context.Entry(warehouse).State = System.Data.Entity.EntityState.Modified;

                    //Check execution of transaction - we expect 1 change to have occurred
                    var execution_result = context.SaveChanges();
                    if (execution_result != 1)
                    {
                        msg = "Warehouse was not updated";
                        ret = false;
                    }
                    else
                    {
                        msg = "Warehouse updated";
                    }
                }
                else
                {
                    msg = "No Warehouse found with the provided ID";
                    ret = false;
                }
            }

            return(ret);
        }