コード例 #1
0
        public static async Task InsertProduct()
        {
            using (var context = new ProductsContext())
            {
                //await context.Products.AddAsync(new Product {
                //    Name = "San pham 1",
                //    Provider = "Cong ty 1"
                //});

                //await context.Products.AddAsync(new Product
                //{
                //    Name = "San pham 2",
                //    Provider = "Cong ty 2"
                //});

                var p1 = new Product()
                {
                    Name = "Sản phẩm 3", Provider = "CTY A"
                };
                var p2 = new Product()
                {
                    Name = "Sản phẩm 4", Provider = "CTY A"
                };
                var p3 = new Product()
                {
                    Name = "Sản phẩm 5", Provider = "CTY B"
                };

                await context.AddRangeAsync(new object[] { p1, p2, p3 });

                int rows = await context.SaveChangesAsync();

                Console.WriteLine($"Da luu {rows} san pham");
            }
        }
コード例 #2
0
        public static async Task RenameProduct(int id, string newName)
        {
            using (var context = new ProductsContext())
            {
                //Timf kiem
                var product = await(from p in context.products
                                    where (p.ProductId == id)
                                    select p).FirstOrDefaultAsync();

                //doi ten
                if (product != null)
                {
                    product.Name = newName;
                    Console.WriteLine($"{product.ProductId} co ten moi {product.Name}");
                    await context.SaveChangesAsync();
                }
            }
        }