Пример #1
0
        private static void DeleteProduct(ContosoPetsContext context, Product product)
        {
            if (product != null)
            {
                context.Remove(product);
            }

            context.SaveChanges();
        }
Пример #2
0
        private static void UpdateProduct(ContosoPetsContext context, Product product)
        {
            if (product != null)
            {
                product.Price = 7.99m;
            }

            context.SaveChanges();
        }
Пример #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 EditRecord()
        {
            using ContosoPetsContext context = new ContosoPetsContext();

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

            if (squeakyBone is Product)
            {
                squeakyBone.Price = 7.99m;
            }

            context.SaveChanges();
        }
Пример #5
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();
        }
Пример #6
0
        static void ContextAddInfo()
        {
            using var context = new ContosoPetsContext();

            var squeakyBone = new Product()
            {
                Name  = "Squeaky Bone Dog",
                Price = 4.99M
            };

            var tennisBall = new Product()
            {
                Name  = "Tennis ball 3-Pack",
                Price = 9.99M
            };

            context.Add(squeakyBone);
            context.Add(tennisBall);

            context.SaveChanges();
        }
Пример #7
0
        private static void SetupDemoProducts()
        {
            using ContosoPetsContext context = new ContosoPetsContext();

            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();
        }
Пример #8
0
        private static void SeedDb(ContosoPetsContext context)
        {
            context.Database.ExecuteSqlRaw("DELETE FROM dbo.Products");
            context.Database.ExecuteSqlRaw("DBCC CHECKIDENT('dbo.Products', RESEED, 0)");

            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();
        }
Пример #9
0
        private ContosoPetsContext GetDatabaseContext()
        {
            var options = new DbContextOptionsBuilder <ContosoPetsContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;
            var databaseContext = new ContosoPetsContext(options);

            databaseContext.Database.EnsureCreated();

            if (databaseContext.Products.Count() <= 0)
            {
                for (int i = 1; i <= 5; i++)
                {
                    databaseContext.Products.Add(
                        new Product(
                            i,
                            $"Product{i}",
                            0.99M + i
                            ));
                    databaseContext.SaveChanges();
                }
            }
            return(databaseContext);
        }
Пример #10
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
        }
Пример #11
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.
             */
        }