private static void AddLensToManufacturer(
     Lens lens, Manufacturer manufacturer, PhotographySystemEntities context)
 {
     if (!manufacturer.Lenses.Contains(lens))
     {
         manufacturer.Lenses.Add(lens);
         context.SaveChanges();
     }
 }
        private static void ImportLensesIfNotExist(
            IEnumerable<XElement> xmlLenses, Manufacturer manufacturer, PhotographySystemEntities context)
        {
            foreach (var xmlLense in xmlLenses)
            {
                XAttribute modelAttr = xmlLense.Attribute("model");
                XAttribute typeAttr = xmlLense.Attribute("type");
                XAttribute pricelAttr = xmlLense.Attribute("price");

                if (modelAttr == null || typeAttr == null)
                {
                    throw new ArgumentException("Model and type of lens is requiered.");
                }

                string model = modelAttr.Value;
                string type = typeAttr.Value;
                decimal? price = null;
                if (pricelAttr != null)
                {
                    price = decimal.Parse(pricelAttr.Value);
                }

                var lensInDb = context.Lenses
                    .FirstOrDefault(l => l.Model == model);

                if (lensInDb == null)
                {
                    Lens newLens = new Lens();
                    newLens.Manufacturer = manufacturer;
                    newLens.Model = model;
                    newLens.Type = type;
                    if (price != null)
                    {
                        newLens.Price = price;
                    }

                    context.Lenses.Add(newLens);
                    context.SaveChanges();
                    Console.WriteLine("Created lens: {0}", model);
                    AddLensToManufacturer(newLens, manufacturer, context);
                }
                else
                {
                    Console.WriteLine("Existing lens: {0}", model);
                    AddLensToManufacturer(lensInDb, manufacturer, context);
                }
            }
        }
        private static void ImportLenses(XElement manufacturerNode, PhotographyEntities context, Manufacturer manufactorer)
        {
            var lenses = manufacturerNode.XPathSelectElements("lenses/lens");

            foreach (var lenseNode in lenses)
            {
                var lensModel = lenseNode.Attribute("model").Value;

                var lens = new Lens();
                if (!context.Lenses.Any(l => l.Model == lensModel))
                {
                    lens.Model = lenseNode.Attribute("model").Value;
                    lens.Type = lenseNode.Attribute("type").Value;

                    if (lenseNode.Attribute("price") != null)
                    {
                        lens.Price = decimal.Parse(lenseNode.Attribute("price").Value);
                    }

                    context.Lenses.Add(lens);
                    if (manufactorer != null)
                    {
                        manufactorer.Lenses.Add(lens);
                    }

                    Console.WriteLine("Created lens: " + lens.Model);
                }
                else
                {
                    lens = context.Lenses.FirstOrDefault(l => l.Model == lensModel);
                    if (lens != null)
                    {
                        Console.WriteLine("Existing lens: " + lens.Model);
                    }
                }
            }
        }