コード例 #1
0
ファイル: ImportXml.cs プロジェクト: peterkirilov/SoftUni-1
        private static void ProcessLenses(PhotographySystemEntities context, XElement lense, Manufacturer manufacturer)
        {
            var modelNode = lense.Attribute("model");
            if (modelNode == null)
            {
                throw new ArgumentException("Model is mandatory for lenses!");
            }

            var model = modelNode.Value;
            var typeNode = lense.Attribute("type");
            if (typeNode == null)
            {
                throw new ArgumentException("Type is required for lenses!");
            }

            var type = typeNode.Value;
            decimal? price = null;
            var priceNode = lense.Attribute("price");
            if (priceNode != null)
            {
                price = decimal.Parse(priceNode.Value);
            }

            Lens processedLense = context.Lenses.FirstOrDefault(l => l.Model == model);
            if (processedLense == null)
            {
                processedLense = new Lens()
                {
                    Model = model,
                    Type = type,
                    Price = price,
                    Manufacturer = manufacturer
                };

                context.Lenses.AddOrUpdate(processedLense);
                context.SaveChanges();
                Console.WriteLine("Created lens: {0}", processedLense.Model);
            }
            else
            {
                Console.WriteLine("Existing lens: {0}", processedLense.Model);
            }
        }
コード例 #2
0
ファイル: ImportXml.cs プロジェクト: peterkirilov/SoftUni-1
        private static Manufacturer ProcessManufacturer(PhotographySystemEntities context, XElement manufacturer)
        {
            var manufacturerNameNode = manufacturer.Element("manufacturer-name");
            if (manufacturerNameNode == null)
            {
                throw new ArgumentException("Manufacturer name is mandatory!");
            }

            var name = manufacturerNameNode.Value;
            var processedManufacturer = context.Manufacturers.FirstOrDefault(m => m.Name == name);
            if (processedManufacturer == null)
            {
                processedManufacturer = new Manufacturer()
                {
                    Name = name
                };

                context.Manufacturers.Add(processedManufacturer);
                context.SaveChanges();
                Console.WriteLine("Created manufacturer: {0}", processedManufacturer.Name);
            }
            else
            {
                Console.WriteLine("Existing manufacturer: {0}", processedManufacturer.Name);
            }

            return processedManufacturer;
        }