Veri Tabanına Kayıt Ekleme

Tek bir kayıt yapma

        static void AddProduct()
        {
            using(var db = new ShopContext())
            {                
                var p = new Product{Name="Iphone 12", Price=15000};
                db.Products.Add(p);
                db.SaveChanges();
            }
        }

Birden çok kayıt ekleme

        static void AddProducts()
        {
            using(var db = new ShopContext())
            {
                var products = new List<Product>
                {
                    new Product{Name="Samsung S10", Price=10000},
                    new Product{Name="Huawei Mate 10 Lite", Price=3500},
                    new Product{Name="Nokia Lumia 520", Price=800},
                    new Product{Name="Xiaomi RedMi S2", Price=2000},
                };

                db.Products.AddRange(products);
                db.SaveChanges();
            }
        }

Last updated