//Parts

        public static int AddPart(partsdb part)
        {
            using (var con = new InventoryMgmtEntities())
            {
                con.partsdbs.Add(part);
                con.SaveChanges();
                return(part.partid);
            }
        }
 public static void RemovePart(int index)
 {
     using (var con = new InventoryMgmtEntities())
     {
         partsdb partTemp = con.partsdbs.Find(index);
         con.partsdbs.Remove(partTemp);
         con.SaveChanges();
     }
 }
 public static void RemoveAssocPartByid(int index)
 {
     using (var con = new InventoryMgmtEntities())
     {
         productspart tempProductPart = con.productsparts.Find(index);
         con.productsparts.Remove(tempProductPart);
         con.SaveChanges();
     }
 }
        //Products

        public static int AddProduct(productsdb product)
        {
            using (var con = new InventoryMgmtEntities())
            {
                con.productsdbs.Add(product);
                con.SaveChanges();
                return(product.productid);
            }
        }
        //Associated Parts

        public static void AddAssocPart(int product, int part)
        {
            using (var con = new InventoryMgmtEntities())
            {
                productspart temp = new productspart();
                temp.productid = product;
                temp.partid    = part;
                con.productsparts.Add(temp);
                con.SaveChanges();
            }
        }
        public static void UpdatePart(partsdb part)
        {
            partsdb updatedPart = part;

            using (var con = new InventoryMgmtEntities())
            {
                partsdb tempPart = con.partsdbs.Find(updatedPart.partid);
                tempPart.name = updatedPart.name; tempPart.instock = updatedPart.instock; tempPart.min = updatedPart.min; tempPart.max = updatedPart.max;
                tempPart.type = updatedPart.type; tempPart.details = updatedPart.details; tempPart.price = updatedPart.price; tempPart.lastmodified = DateTime.Now;

                if (updatedPart.createdby != null)
                {
                    tempPart.createdby = updatedPart.createdby;
                }

                con.SaveChanges();
            }
        }
        public static bool ModifyProductImproved(productsdb product, List <partsdb> partsList, List <productspart> originalParts)
        {
            using (var con = new InventoryMgmtEntities())
            {
                //Update Part Details
                productsdb productTemp = con.productsdbs.Find(product.productid);;
                productTemp.name  = product.name; productTemp.min = product.min; productTemp.max = product.max; productTemp.instock = product.instock;
                productTemp.price = product.price; productTemp.lastmodified = DateTime.Now;

                //Split list into associated parts that already exist and new parts
                List <partDerived> partDerivedList = new List <partDerived>();
                List <partsdb>     realParts       = new List <partsdb>();
                for (int i = 0; i < partsList.Count(); i++)
                {
                    if (partsList[i].GetType() == typeof(partDerived))
                    {
                        partDerived tempPart = (partDerived)(object)partsList[i];
                        partDerivedList.Add(tempPart);
                    }
                    else
                    {
                        realParts.Add(partsList[i]);
                    }
                }

                //Take original list and derived part list, where there is a match remove part from partListfinal
                List <productspart> partListFinal = originalParts;

                for (int i = 0; i < originalParts.Count(); i++)
                {
                    for (int j = 0; j < partDerivedList.Count(); j++)
                    {
                        if (originalParts[i].productpartid == partDerivedList[j].zproductpartid)
                        {
                            partListFinal.RemoveAll(u => u.productpartid == (int)partDerivedList[j].zproductpartid);
                        }
                    }
                    var temp = partListFinal;
                }

                //Remove all productparts from the db that are in the list partListFinal
                if (partListFinal.Count() > 0)
                {
                    for (int k = 0; k < partListFinal.Count(); k++)
                    {
                        productspart tempProductPart = con.productsparts.Find(partListFinal[k].productpartid);
                        con.productsparts.Remove(tempProductPart);
                    }
                }

                //Add new associated parts
                for (int l = 0; l < realParts.Count(); l++)
                {
                    productspart temp = new productspart();
                    temp.productid = product.productid;
                    temp.partid    = realParts[l].partid;
                    con.productsparts.Add(temp);
                }
                con.SaveChanges();
                return(true);
            }
        }