コード例 #1
0
        private static void DeleteProduct(ContosoPetsContext context, Product product)
        {
            if (product != null)
            {
                context.Remove(product);
            }

            context.SaveChanges();
        }
コード例 #2
0
        // Add the Delete code
        public async Task <bool> Delete(int id)
        {
            var isDeleted = false;
            var order     = await GetOrderById(id).FirstOrDefaultAsync();

            if (order != null)
            {
                context.Remove(order);
                await context.SaveChangesAsync();

                isDeleted = true;
            }
            return(isDeleted);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            using ContosoPetsContext context = new ContosoPetsContext();

            var squeakyBone = context.Products
                              .Where(p => p.Name == "Squeaky Dog Bone")
                              .FirstOrDefault();

            if (squeakyBone is Product)
            {
                //Edit entity records in the database
                //squeakyBone.Price = 7.99M;
                //Delete entity records from the database
                context.Remove(squeakyBone);
            }
            context.SaveChanges();

            //Display entity records from the database
            var products = context.Products
                           .Where(p => p.Price >= 5.00M)
                           .OrderBy(p => p.Name);

            foreach (Product p in products)
            {
                Console.WriteLine($"Id: {p.Id}");
                Console.WriteLine($"Name: {p.Name}");
                Console.WriteLine($"Price {p.Price}");
                Console.WriteLine(new string('-', 20));
            }



            //Add New products into database

            //Product squeakyBone = new Product()
            //{
            //    Name = "Squeaky Dog Bone",
            //    Price = 4.99M
            //};
            //context.Products.Add(squeakyBone);

            //Product tennisBalls = new Product()
            //{
            //    Name = "Tennis Ball 3-Pack",
            //    Price = 9.99M
            //};
            //context.Add(tennisBalls);

            //context.SaveChanges();
        }
コード例 #4
0
        private static void DeleteRecord()
        {
            using ContosoPetsContext context = new ContosoPetsContext();

            var squeakyBone = context.Products
                              .Where(p => p.Name == "Squeaky Dog Bone")
                              .FirstOrDefault();

            if (squeakyBone is Product)
            {
                context.Remove(squeakyBone);
            }

            context.SaveChanges();
        }
コード例 #5
0
ファイル: OrderService.cs プロジェクト: scottaddie/RetailApi
        public async Task <bool> Delete(int id)
        {
            bool  isDeleted = false;
            Order order     = await GetOrderById(id)
                              .Include(o => o.ProductOrder)
                              .FirstOrDefaultAsync();

            if (order != null)
            {
                _context.Remove(order);
                await _context.SaveChangesAsync();

                isDeleted = true;
            }

            return(isDeleted);
        }
コード例 #6
0
        static void Main(string[] args)
        {
            using ContosoPetsContext context = new ContosoPetsContext();

            var squeakyBone = context.Products
                              .Where(p => p.Name == "Squeaky Dog Bone")
                              .FirstOrDefault();

            if (squeakyBone is Product)
            {
                // step 3 - update the price
                // squeakyBone.Price = 7.99M;

                context.Remove(squeakyBone);
            }
            context.SaveChanges();

            // step 1 - Product creation

            /*
             * Product squeakyBone = new Product()
             * {
             *   Name = "Squeacky Dog Bone",
             *   Price = 4.99M
             * };
             * context.Products.Add(squeakyBone);
             *
             * Product tennisBalls = new Product()
             * {
             *   Name = "Tennis Ball 3-Pack",
             *   Price = 9.99M
             * };
             * context.Add(tennisBalls);
             *
             * context.SaveChanges();
             */
            // end step 1

            // step 2 - read and output to console.
            var products = context.Products
                           .Where(p => p.Price >= 5.00M)
                           .OrderBy(p => p.Name);

            /*
             * // alternative using LINQ. Use the one above OR below, not both
             *
             * var products = from product in context.Products
             *             where product.Price > 5.00M
             *             orderby product.Name
             *             select product;
             *
             */
            foreach (Product p in products)
            {
                Console.WriteLine($"Id: {p.Id}");
                Console.WriteLine($"Name: {p.Name}");
                Console.WriteLine($"Price: {p.Price}");
                Console.WriteLine(new String('-', 20));
            }

            // end step 2
        }
コード例 #7
0
        static void Main(string[] args)
        {
            // We create a new instance of ContosoPetsContext with using statement so that we ensure ContosoPetsContext objetc is disposed off properly after we're done using it.
            using ContosoPetsContext context = new ContosoPetsContext();



            /*
             * // Editing Data
             * var squeakyBone = context.Products
             *                  .Where(p => p.Name == "Squeaky Dog Bone")
             *                  .FirstOrDefault(); // incase we don't have the record we get null
             *
             * if (squeakyBone is Product) // To check to see if squeakyBone is a Product
             * {
             *  squeakyBone.Price = 7.99m; // if it is a product we set the price for 7.99m
             * }
             * context.SaveChanges();
             */

            // Deleting a data from the database
            var squeakyBone = context.Products
                              .Where(p => p.Name == "Squeaky Dog Bone")
                              .FirstOrDefault(); // incase we don't have the record we get null

            if (squeakyBone is Product)          // To check to see if squeakyBone is a Product
            {
                context.Remove(squeakyBone);     // if it is a product we set the price for 7.99m
            }
            context.SaveChanges();



            // LINQ method does the same thing as the Fluent API in the below example
            var products = from product in context.Products
                           where product.Price > 5.00m
                           orderby product.Name
                           select product;


            /*
             * // Fluent APIs use extension methods that chain methods together and lamda (p=> p.) expressions to specify the query
             * var products = context.Products
             *  .Where(p => p.Price >= 5.00m)
             *  .OrderBy(p => p.Name);
             */



            // Reading Data to console from database

            foreach (Product p in products)
            {
                Console.WriteLine($"Id: {p.Id}");
                Console.WriteLine($"Name: {p.Name}");
                Console.WriteLine($"Price: {p.Price}");
                Console.WriteLine(new string('-', 20));
            }



            /*
             * // Adding data to database
             *
             * Product squeakyBone = new Product()
             * {
             *  Name = "Squeaky Dog Bone",
             *  Price = 4.99M
             * };
             * context.Products.Add(squeakyBone);
             *
             * Product tennisBalls = new Product()
             * {
             *  Name = "Tennis Ball 3-Pack",
             *  Price = 9.99M
             * };
             * context.Add(tennisBalls); // Here we don't have to use the context.Products.Add since entity framework knows that is a product value
             *
             * context.SaveChanges();
             * // now after using it we can erase and then run the code up there to call the data to our console.
             */
        }