Exemple #1
0
        public void UpdateBag(Bag uptBag)
        {
            Bag bag = SLTDbContext.Bags.Include(b => b.BagsColors).
                      Where(b => b.BagId == uptBag.BagId).FirstOrDefault();

            if (bag != null)
            {
                foreach (var colorItem in bag.BagsColors.ToList())
                {
                    SLTDbContext.BagsColors.Remove(colorItem);
                }
                SLTDbContext.Bags.AddOrUpdate(uptBag);
                foreach (var item in uptBag.BagsPictures)
                {
                    item.BagId = uptBag.BagId;

                    SLTDbContext.Pictures.Add(item);
                }


                foreach (var item in uptBag.ColorList)
                {
                    if (item.isChecked == true)
                    {
                        BagsColors bc = new BagsColors();
                        bc.BagId   = uptBag.BagId;
                        bc.ColorId = item.Id;
                        SLTDbContext.BagsColors.Add(bc);
                    }
                }

                //SLTDbContext.Entry(bag).State = EntityState.Modified;
                SLTDbContext.SaveChanges();
            }
        }
Exemple #2
0
        public Bag AddBag(Bag newBag)
        {
            SLTDbContext.Bags.Add(newBag);


            foreach (var item in newBag.ColorList)
            {
                if (item.isChecked == true)
                {
                    BagsColors bc = new BagsColors();
                    bc.BagId   = newBag.BagId;
                    bc.ColorId = item.Id;
                    SLTDbContext.BagsColors.Add(bc);
                }
            }
            SLTDbContext.SaveChanges();

            return(newBag);
        }
        protected override void Seed(SLT.SLTDbContext dbContext)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.
            var bagCategories = new List <Category>
            {
                new Category {
                    CatName = "Tote Bag"
                },
                new Category {
                    CatName = "Shopping Bag"
                },
                new Category {
                    CatName = "Shoulder Bag"
                }
            };

            bagCategories.ForEach(c => dbContext.Categories.AddOrUpdate(cat => cat.CatName, c));
            SaveChanges(dbContext);
            //Seeding bags
            var bags = new List <Bag>
            {
                new Bag {
                    BagBrand = "LV", Cost = 1000, sellCost = 1500, Quantity = 50, CategoryId = 1
                },
                new Bag {
                    BagBrand = "Channel", Cost = 2000, sellCost = 2500, Quantity = 60, CategoryId = 2
                }
            };

            bags.ForEach(b => dbContext.Bags.AddOrUpdate(p => p.BagBrand, b));
            SaveChanges(dbContext);

            //seeding colors
            var colors = new List <Color>
            {
                new Color {
                    ColorName = "Red"
                },
                new Color {
                    ColorName = "Green"
                },
                new Color {
                    ColorName = "Yellow"
                }
            };

            colors.ForEach(c => dbContext.Colors.AddOrUpdate(p => p.ColorName, c));
            SaveChanges(dbContext);

            //seeding bagcolor
            var bagsColors = new BagsColors[] {
                new BagsColors
                {
                    BagId   = bags.Single(b => b.BagBrand == "Channel").BagId,
                    ColorId = colors.Single(c => c.ColorName == "Red").Id
                },
                new BagsColors
                {
                    BagId   = bags.Single(b => b.BagBrand == "LV").BagId,
                    ColorId = colors.Single(c => c.ColorName == "Green").Id
                },
            };

            foreach (BagsColors bc in bagsColors)
            {
                dbContext.BagsColors.AddOrUpdate(bc);
            }

            SaveChanges(dbContext);


            base.Seed(dbContext);
        }