示例#1
0
        private void LoadProducts(IEnumerable<XElement> items)
        {
            int i = 0;
            String StringTags = "";
            foreach (var item in items)
            {
                int id = Convert.ToInt32(item.Attribute("id").Value);
                int categoryId = Convert.ToInt32(item.Element("categoryId").Value);

                var vendorName = ReadAttribute(item, "vendor");
                if (!vendorNames.ContainsKey(vendorName))
                {
                    VendorName vendor = new VendorName() { Name = vendorName };
                    vendorNames[vendorName] = vendor;
                    dataContext.VendorNames.Add(vendor);
                }

                var vendorCode = ReadAttribute(item, "vendorCode");
                if (!vendors.ContainsKey(vendorCode))
                {
                    Vendor vendor = new Vendor() { VendorCode = vendorCode, VendorName = vendorNames[vendorName] };
                    vendors[vendorCode] = vendor;
                    dataContext.Vendors.Add(vendor);
                }

                var listPicture = new List<Picture>();
                foreach (XElement pic in item.Elements("picture"))
                {
                    Picture picture = new Picture
                    {
                        PictureUrl = pic.Value,
                        ProductId = id
                    };
                    dataContext.Pictures.Add(picture);
                    listPicture.Add(picture);
                }

                StringTags += dataContext.Categories.Find(categoryId).Name + " ";
                StringTags += vendorName;

                List<Tag> tags = new List<Tag>();
                foreach (var tag in new HelpTableTag().GetTagsFromString(StringTags))
                {
                    tags.Add(dataContext.Tags.Find(tag.TagId));
                }
                double price =0;

                try
                {
                    price = Convert.ToDouble(ReadAttribute(item, "price"));
                }
                catch { }

                var product = new Product()
                {
                    ProductId = id,
                    Barcode = item.GetStringValue("barcode"),
                    CategoryId = categoryId,
                    Currency = item.GetEntity("currencyId", currencies),
                    Description = item.GetStringValue("description"),
                    Discount = 0.0,
                    Name = item.GetStringValue("name"),
                    Price = price,
                    Url = item.GetStringValue("url"),
                    Vendor = vendors[vendorCode],
                    Picture = listPicture,
                    Tags = tags
                };

                StringTags = "";
                dataContext.Products.Add(product);
                dataContext.SaveChanges();

                foreach (XElement element in item.Elements("param"))
                {
                    var paramName = element.Attribute("name").Value;
                    if (!paramNames.ContainsKey(paramName))
                    {
                        ParamName param = new ParamName() { Name = paramName };
                        dataContext.ParamNames.Add(param);
                        dataContext.SaveChanges();
                        paramNames[paramName] = dataContext.ParamNames.First(x => x.Name == paramName);
                    }
                    var paramValue = element.Value;
                    if (!paramValues.ContainsKey(paramValue))
                    {
                        ParamValue param = new ParamValue() { Name = paramValue };
                        dataContext.ParamValues.Add(param);
                        dataContext.SaveChanges();
                        paramValues[paramValue] = dataContext.ParamValues.First(x => x.Name == paramValue);
                    }

                    dataContext.Parameters.Add(new Parameter()
                    {
                        ParamValueId = paramValues[paramValue].ParamValueId,
                        ParamNameId = paramNames[paramName].ParamNameId,
                        ProductId = id
                    });
                }
                if (price != 0)
                    dataContext.Store.Add(new Store() { ProductId = id, Quantity = 2 });

                dataContext.SaveChanges();

                if (++i % 10!= 0)
                {
                    continue;
                }
                Console.WriteLine("Commit product: {0}", i);
                if(i==2000)
                    break;
            }
            dataContext.SaveChanges();
        }
示例#2
0
        private void LoadProducts(IEnumerable<XElement> items)
        {
            int i = 0;
            foreach (var item in items)
            {
                int id = Convert.ToInt32(item.Attribute("id").Value);
                int categoryId = Convert.ToInt32(item.Element("categoryId").Value);

                var vendorName = ReadAttribute(item, "vendor");
                if (!vendorNames.ContainsKey(vendorName))
                {
                    VendorName vendor = new VendorName() { Name = vendorName };
                    vendorNames[vendorName] = vendor;
                    dataContext.VendorNames.Add(vendor);
                }

                var vendorCode = ReadAttribute(item, "vendorCode");
                if (!vendors.ContainsKey(vendorCode))
                {
                    Vendor vendor = new Vendor() { VendorCode = vendorCode, VendorName = vendorNames[vendorName] };
                    vendors[vendorCode] = vendor;
                    dataContext.Vendors.Add(vendor);
                }

                var listPicture = new List<Picture>();
                foreach (XElement pic in item.Elements("picture"))
                {
                    Picture picture = new Picture
                    {
                        PictureUrl = pic.Value,
                        ProductId = id
                    };
                    dataContext.Pictures.Add(picture);
                    listPicture.Add(picture);
                }

                var product = new Product()
                {
                    ProductId = id,
                    Barcode = item.GetStringValue("barcode"),
                    CategoryId = categoryId,
                    Currency = item.GetEntity("currencyId", currencies),
                    Description = item.GetStringValue("description"),
                    Discount = 0.0,
                    Name = item.GetStringValue("name"),
                    Price = Convert.ToDouble(item.Attribute("price")),
                    Url = item.GetStringValue("url"),
                    Vendor = vendors[vendorCode],
                    Picture = listPicture
                };
                dataContext.Products.Add(product);
                dataContext.SaveChanges();

                foreach (XElement element in item.Elements("param"))
                {
                    var paramName = element.Attribute("name").Value;
                    if (!paramNames.ContainsKey(paramName))
                    {
                        ParamName param = new ParamName() { Name = paramName };
                        dataContext.ParamNames.Add(param);
                        dataContext.SaveChanges();
                        paramNames[paramName] = dataContext.ParamNames.First(x => x.Name == paramName);
                    }
                    var paramValue = element.Value;
                    if (!paramValues.ContainsKey(paramValue))
                    {
                        ParamValue param = new ParamValue() { Name = paramValue };
                        dataContext.ParamValues.Add(param);
                        dataContext.SaveChanges();
                        paramValues[paramValue] = dataContext.ParamValues.First(x => x.Name == paramValue);
                    }

                    dataContext.Parameters.Add(new Parameter()
                    {
                        ParamNameId = paramValues[paramValue].ParamValueId,
                        ParamValueId = paramNames[paramName].ParamNameId,
                        ProductId = id
                    });
                }
                dataContext.SaveChanges();
                if (++i % 100 != 0)
                {
                    continue;
                }
                Console.WriteLine("Commit books: {0}", i);
            }
            dataContext.SaveChanges();
        }
示例#3
0
        public static void AddProduct()
        {
            using (var dbContext = new DataContext("DBShop1"))
            {
                Category cat1 = new Category() { CategoryId = 1, Name = category1, Parent = null };
                dbContext.Categories.Add(cat1);
                dbContext.SaveChanges();

                int productId = 123123;
                int categoryId = 1;
                string vendorName = "Sumsung";
                VendorName vendorN = dbContext.VendorNames.FirstOrDefault(x=>x.Name == vendorName);
                if (vendorN == null)
                {
                    VendorName vendor = new VendorName() { Name = vendorName };
                    dbContext.VendorNames.Add(vendor);
                }

                var vendorCode = "GT-5000";
                Vendor vendorV = dbContext.Vendors.FirstOrDefault(x=>x.VendorCode == vendorCode);
                if (vendorV == null)
                {
                    vendorV = new Vendor() { VendorCode = vendorCode, VendorName = vendorN };
                    dbContext.Vendors.Add(vendorV);
                }

                var listPicture = new List<Picture>();
                for(int i = 0; i<3; i++)
                {
                    Picture picture = new Picture { PictureUrl = "pic" + i, ProductId = productId };
                    dbContext.Pictures.Add(picture);
                    listPicture.Add(picture);
                }

                var product = new Product()
                {
                    ProductId = productId,
                    Barcode = "barcode",
                    CategoryId = categoryId,
                    Currency = dbContext.Currencies.Find(1),
                    Description = "description",
                    Discount = 0.0,
                    Name = "name",
                    Price = 123.6,
                    Url = "url\\",
                    Vendor = vendorV,
                    Picture = listPicture
                };
                dbContext.Products.Add(product);
                dbContext.SaveChanges();

                for (int i = 0; i < 4; i++ )
                {
                    var paramName = "name" + i;
                    ParamName paramN = dbContext.ParamNames.FirstOrDefault(x => x.Name == paramName);
                    if (paramN == null)
                    {
                        paramN = new ParamName() { Name = paramName };
                        dbContext.ParamNames.Add(paramN);
                        dbContext.SaveChanges();
                    }
                    var paramValue = "Value" + i;
                    ParamValue paramV = dbContext.ParamValues.FirstOrDefault(x => x.Name == paramValue);
                    if (paramV == null)
                    {
                        paramV= new ParamValue() { Name = paramValue };
                        dbContext.ParamValues.Add(paramV);
                        dbContext.SaveChanges();
                    }

                    dbContext.Parameters.Add(new Parameter()
                    {
                        ParamNameId = paramV.ParamValueId,
                        ParamValueId = paramN.ParamNameId,
                        ProductId = productId
                    });
                }
                dbContext.SaveChanges();

                Assert.AreEqual(1, dbContext.Products.Count());
                Assert.AreEqual(3, dbContext.Pictures.Count());
                Assert.AreEqual(4, dbContext.Parameters.Count());
                Assert.AreEqual(1, dbContext.Vendors.Count());
                Assert.AreEqual(1, dbContext.VendorNames.Count());
                Assert.AreEqual(1, dbContext.Categories.Count());

                dbContext.Products.Remove(product);
                dbContext.SaveChanges();

                Assert.AreEqual(0, dbContext.Products.Count());
                Assert.AreEqual(0, dbContext.Pictures.Count());
                Assert.AreEqual(0, dbContext.Parameters.Count());
                Assert.AreEqual(1, dbContext.Categories.Count());

                dbContext.Categories.Remove(cat1);
                dbContext.SaveChanges();
                Assert.AreEqual(0, dbContext.Categories.Count());
            }
        }