public void AddProduct(ProductInformation newProduct) { using (db) { //Could have a List<ProductInformation> instead - to add more than one decimal property at a time db.Product.Add(new Product { Name = newProduct.Name, Price = newProduct.Price, Brand = newProduct.Brand, Color = newProduct.Color, Description = newProduct.Description, Height = newProduct.Height, ImagePath = newProduct.ImagePath, Length = newProduct.Length, Balance = newProduct.Balance, AnimalCategory = newProduct.AnimalCategory }); db.SaveChanges(); } }
public void EditProduct(ProductInformation product) { using (db) { //Fetching the products id, to be able to edit the right product Product editedProduct = db.Product.Find(product.Id); //Then the new values of the properties are stored in the new variable if (product.Name != null) { editedProduct.Name = product.Name; } if (product.Balance != null) { editedProduct.Balance = product.Balance; } if (product.Brand != null) { editedProduct.Brand = product.Brand; } if (product.Color != null) { editedProduct.Color = product.Color; } if (product.Description != null) { editedProduct.Description = product.Description; } if (product.Height != null) { editedProduct.Height = product.Height; } if (product.ImagePath != null) { editedProduct.ImagePath = product.ImagePath; } if (product.Length != null) { editedProduct.Length = product.Length; } if (product.Price != null) { editedProduct.Price = product.Price; } if (product.AnimalCategory != null) { editedProduct.AnimalCategory = product.AnimalCategory; } db.Entry(editedProduct).State = EntityState.Modified; db.SaveChanges(); } }