/// <summary>
 /// Deprecated Method for adding a new object to the Products EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToProducts(Product product)
 {
     base.AddObject("Products", product);
 }
 /// <summary>
 /// Create a new Product object.
 /// </summary>
 /// <param name="productID">Initial value of the ProductID property.</param>
 /// <param name="productName">Initial value of the ProductName property.</param>
 /// <param name="discontinued">Initial value of the Discontinued property.</param>
 public static Product CreateProduct(global::System.Int32 productID, global::System.String productName, global::System.Boolean discontinued)
 {
     Product product = new Product();
     product.ProductID = productID;
     product.ProductName = productName;
     product.Discontinued = discontinued;
     return product;
 }
Example #3
0
        static void TestTables()
        {
            using (NorthwindEntities NWEntities = new NorthwindEntities())
            {
                // retrieve all Beverages products
                IEnumerable<Product> beverages =
                    from p in NWEntities.Products
                    where p.Category.CategoryName == "Beverages"
                    orderby p.ProductName
                    select p;
                Console.WriteLine("There are {0} Beverages",
                    beverages.Count());

                // update one product
                Product bev1 = beverages.ElementAtOrDefault(10);
                if (bev1 != null)
                {
                    decimal newPrice = (decimal)bev1.UnitPrice + 10.00m;
                    Console.WriteLine("The price of {0} is {1}. Update to {2}",
                        bev1.ProductName, bev1.UnitPrice, newPrice);
                    bev1.UnitPrice = newPrice;
                }

                // submit the change to database
                NWEntities.SaveChanges();

                // insert a product
                Product newProduct = new Product
                {
                    ProductName =
                        "new test product"
                };
                NWEntities.Products.Add(newProduct);
                NWEntities.SaveChanges();

                Console.WriteLine("Added a new product");

                // delete a product
                IQueryable<Product> productsToDelete =
                         from p in NWEntities.Products
                         where p.ProductName == "new test product"
                         select p;
                if (productsToDelete.Count() > 0)
                {
                    foreach (var p in productsToDelete)
                    {
                        NWEntities.Products.Remove(p);
                        Console.WriteLine("Deleted product {0}",
                               p.ProductID);
                    }
                    NWEntities.SaveChanges();
                }
            }
        }