public static void DbSetUpdateDemo(KTStoreModel context) { var product = context.Product.First(); product.Price = 10000m; context.SaveChanges(); context.Entry(product).Reload(); product = context.Product.First(); Console.WriteLine($"{product.Name}:{product.Price}"); product.Price = 39000m; context.SaveChanges(); context.Entry(product).Reload(); product = context.Product.First(); Console.WriteLine($"{product.Name}:{product.Price}"); }
public static void AddData(KTStoreModel context) { if (context.Product.Count() <= 5) { var data1 = new Product() { Name = "ASUS", Price = 39000M, Quantity = 1 }; var data2 = new Product() { Name = "APPLE MAC", Price = 60000M, Quantity = 1 }; var data3 = new Product() { Name = "LENOVO", Price = 32000M, Quantity = 1 }; var data4 = new Product() { Name = "HP", Price = 39000M, Quantity = 1 }; context.Product.AddRange(new List <Product>() { data1, data2, data3, data4 }); context.SaveChanges(); } }
public static void DbSetRemoveDemo(KTStoreModel context) { var product = new Product() //跑錯棚的商品 { Name = "統一科學麵", Price = 20, Quantity = 1 }; context.Product.Add(product); //做假資料 context.SaveChanges(); context.Entry(product).Reload(); var count = context.Product.Where(p => p.Name == "統一科學麵").Count(); Console.WriteLine($"資料筆數:{count}"); var removeData = context.Product.First(p => p.Name == "統一科學麵"); context.Product.Remove(removeData); context.SaveChanges(); count = context.Product.Where(p => p.Name == "統一科學麵").Count(); Console.WriteLine($"資料筆數:{count}"); }