コード例 #1
0
        private static void ImportJsonProducts(ShopContext context)
        {
            StreamReader reader = new StreamReader("../../products.json");
            List<Product> products = JsonConvert.DeserializeObject<List<Product>>(reader.ReadToEnd());
            reader.Close();

            Random randGenerator = new Random();
            foreach (var product in products)
            {
                if ((randGenerator.Next(1, 100) < 10))
                {
                    product.Buyer = context.Users.Find(randGenerator.Next(1, context.Users.Count() + 1));
                }

                product.Seller = context.Users.Find(randGenerator.Next(1, context.Users.Count() + 1));

                var category = context.Categories.Find(randGenerator.Next(1, context.Categories.Count() + 1));
                product.Categories.Add(category);

                context.Products.Add(product);
                context.SaveChanges();
            }
        }
コード例 #2
0
        private static void ImportXmlUsers(ShopContext context)
        {
            XmlDocument inputUsersXml = new XmlDocument();
            inputUsersXml.Load("../../users.xml");
            XmlNodeList xUsers = inputUsersXml.SelectNodes("/users/user");

            foreach (XmlNode xUser in xUsers)
            {
                User user = new User();

                if (xUser.Attributes["first-name"] != null)
                {
                    user.FirstName = xUser.Attributes["first-name"].Value;
                }

                if (xUser.Attributes["last-name"] != null)
                {
                    user.LastName = xUser.Attributes["last-name"].Value;
                }

                if (xUser.Attributes["age"] != null)
                {
                    user.Age = int.Parse(xUser.Attributes["age"].Value);
                }

                context.Users.Add(user);
                context.SaveChanges();
            }
        }
コード例 #3
0
        private static void ImportJsonCategories(ShopContext context)
        {
            StreamReader reader = new StreamReader("../../categories.json");
            List<Category> categories = JsonConvert.DeserializeObject<List<Category>>(reader.ReadToEnd());
            reader.Close();

            foreach (var category in categories)
            {
                context.Categories.Add(category);
            }

            context.SaveChanges();
        }