Esempio n. 1
0
        static void AddProducts()
        {
            // Çoklu veri ekleme
            using (var db = new SalihContext())
            {
                var products = new List <Product>
                {
                    new Product {
                        Name = "Tel1", Price = 200
                    },
                    new Product {
                        Name = "Tel2", Price = 300
                    },
                    new Product {
                        Name = "Tel3", Price = 400
                    },
                    new Product {
                        Name = "Tel4", Price = 500
                    }
                };

                //foreach (var p in products)
                //{
                //    db.Products.Add(p);
                //}

                // veya direk AddRange ile

                db.Products.AddRange(products);
                db.SaveChanges();
                Console.WriteLine("Veriler eklendi");
            }
        }
Esempio n. 2
0
        static void DeleteProduct(string name)
        {
            using (var db = new SalihContext())
            {
                var p = db.Products.FirstOrDefault(i => i.Name == name);

                if (p != null)
                {
                    db.Remove(p);
                    db.SaveChanges();

                    Console.WriteLine("Kayıt silindi");


                    // 2. yöntem select olmadan çalışır direk siler


                    /*
                     *  var p1 = new Product() { Name = "Tel1" };
                     *  //db.Products.Remove(p1);
                     *  // veya
                     *  db.Entry(p1).State = EntityState.Deleted;
                     *  db.SaveChanges();
                     */
                }
            }
        }
Esempio n. 3
0
        static void InsertAddresses()
        {
            var addresses = new List <Address>()
            {
                new Address()
                {
                    FullName = "Adres1", Title = "Ev", Body = "Gebze", UserId = 1
                },
                new Address()
                {
                    FullName = "Adres1-2", Title = "iş", Body = "işgebze", UserId = 1
                },
                new Address()
                {
                    FullName = "Adres2", Title = "Ev", Body = "Gebze-ev", UserId = 2
                },
                new Address()
                {
                    FullName = "Adres2-2", Title = "iş", Body = "Gebze-iş", UserId = 2
                },
                new Address()
                {
                    FullName = "Adres3", Title = "Ev", Body = "Gebze-ev", UserId = 3
                }
            };

            using (var db = new SalihContext())
            {
                db.Addresses.AddRange(addresses);
                db.SaveChanges();
            }
        }
Esempio n. 4
0
        static void GetProductByName(string name)
        {
            using (var db = new SalihContext())
            {
                var product = db
                              .Products
                              .Where(product =>
                                     product.Name == name
                                     )
                              .FirstOrDefault();


                Console.WriteLine(product.ProductId + " " + product.Name + " " + product.Price);
            }
        }
Esempio n. 5
0
        static void UpdateProduct(string id)
        {
            using (var db = new SalihContext())
            {
                // change tracking
                var p = db.Products.Where(i => i.ProductId.ToString() == id).FirstOrDefault();
                if (p != null)
                {
                    p.Price *= 1.2m;

                    db.SaveChanges();

                    Console.WriteLine("güncelleme yapıldı");
                }
            }
        }
Esempio n. 6
0
        static void AddProduct()
        {
            // Veri ekleme

            using (var db = new SalihContext())
            {
                var product = new Product {
                    Name = "PC", Price = 2000
                };


                db.Products.Add(product);

                db.SaveChanges();

                Console.WriteLine("Veri eklendi");
            }
        }
Esempio n. 7
0
        static void UpdateByName(string name)
        {
            // 2. yöntem
            using (var db = new SalihContext())
            {
                var entity = new Product()
                {
                    Name = name
                };
                db.Products.Attach(entity);
                entity.Price = 1999;

                db.SaveChanges();


                Console.WriteLine("güncelleme yapıldı");
            }
        }
Esempio n. 8
0
        static void GetAllProducts()
        {
            using (var db = new SalihContext())
            {
                var products = db
                               .Products
                               .Select(product =>
                                       new
                {
                    product.Name,
                    product.Price
                })
                               .ToList();

                foreach (var p in products)
                {
                    Console.WriteLine(p.Name + " " + p.Price);
                }
            }
        }
Esempio n. 9
0
        static void InsertUsers()
        {
            var users = new List <User>()
            {
                new User()
                {
                    Username = "******", Email = "*****@*****.**"
                },
                new User()
                {
                    Username = "******", Email = "*****@*****.**"
                },
                new User()
                {
                    Username = "******", Email = "*****@*****.**"
                }
            };

            using (var db = new SalihContext())
            {
                db.Users.AddRange(users);
                db.SaveChanges();
            }
        }
Esempio n. 10
0
        public static void InsertManyToMany()
        {
            using (var db = new SalihContext())
            {
                var blogs = new List <Blog>()
                {
                    new Blog()
                    {
                        Name = "b1", Content = "test blog1"
                    },
                    new Blog()
                    {
                        Name = "b2", Content = "test blog2"
                    },
                    new Blog()
                    {
                        Name = "b3", Content = "test blog3"
                    }
                };

                db.Blogs.AddRange(blogs);
                db.SaveChanges();

                var comments = new List <Comment>()
                {
                    new Comment()
                    {
                        CommentText = "test yorum1", Commenter = "salih"
                    },
                    new Comment()
                    {
                        CommentText = "test yorum2", Commenter = "salih1"
                    },
                    new Comment()
                    {
                        CommentText = "test yorum3", Commenter = "salih2"
                    },
                    new Comment()
                    {
                        CommentText = "test yorum4", Commenter = "salih3"
                    },
                    new Comment()
                    {
                        CommentText = "test yorum5", Commenter = "salih4"
                    },
                    new Comment()
                    {
                        CommentText = "test yorum6", Commenter = "salih5"
                    },
                    new Comment()
                    {
                        CommentText = "test yorum7", Commenter = "salih5"
                    },
                    new Comment()
                    {
                        CommentText = "test yorum8", Commenter = "salih7"
                    }
                };

                db.Comments.AddRange(comments);
                db.SaveChanges();
            }
        }