コード例 #1
0
        internal void Populate(HPlusSportsDbContext context, XDocument doc)
        {
            var categories = doc.Descendants("Categories").Descendants("Category")
                             .Select(x => new Category
            {
                Key   = ToKey(x.Element("Name").Value),
                Name  = x.Element("Name").Value,
                Image = new Image {
                    Url = x.Element("ImageUrl").Value
                },
            })
                             .ToArray();

            context.Images.AddRange(categories.Select(x => x.Image));
            context.SaveChanges();

            context.Categories.AddRange(categories);
            context.SaveChanges();

            var products = doc.Descendants("Product")
                           .Select(x =>
            {
                var product = new Product
                {
                    CategoryId        = categories.First(cat => cat.Name == x.Element("Category").Value).Id,
                    Name              = x.Element("Name").Value,
                    Description       = StripHtml(x.Element("Description").Value),
                    MSRP              = double.Parse(x.Element("MSRP").Value),
                    Price             = double.Parse(x.Element("Price").Value),
                    SKU               = x.Element("SKU").Value,
                    Summary           = x.Element("Summary").Value,
                    LastUpdated       = DateTime.UtcNow,
                    LastUpdatedUserId = "*****@*****.**",
                    ThumbnailImage    = new Image {
                        Url = x.Element("ThumbnailImageUrl").Value
                    },
                };

                product.Images.Add(new Image {
                    Url = x.Element("ImageUrl").Value
                });

                return(product);
            })
                           .ToArray();

            context.Products.AddRange(products);
            context.SaveChanges();

            var reviews = context.Products.SelectMany(GenerateReviews);

            context.Reviews.AddRange(reviews);
            context.SaveChanges();
        }
コード例 #2
0
        public void Populate(HPlusSportsDbContext context)
        {
            var doc = ReadTestData();

            Populate(context, doc);
        }