private static Manufacturer CreateManufacturerIfNotExists(PhotographySystemEntities context, XElement xManufacturer)
        {
            Manufacturer manufacturer = null;
            var xManifName = xManufacturer.Element("manufacturer-name");

            if (xManifName != null)
            {
                var manifacName = xManifName.Value;

                manufacturer = context.Manufacturers.FirstOrDefault(m => m.Name == manifacName);
                if (manufacturer != null)
                {
                    Console.WriteLine("Existing manufacturer: {0}", manifacName);
                }
                else
                {
                    manufacturer = new Manufacturer
                    {
                        Name = manifacName
                    };

                    context.Manufacturers.Add(manufacturer);
                    context.SaveChanges();
                    Console.WriteLine("Created manufacturer: {0}", manifacName);
                }
            }
            return manufacturer;
        }
        private static void CreateLensesIfNotExist(PhotographySystemEntities context, IEnumerable<XElement> xLenses, Manufacturer manufacturer)
        {
            foreach (var xLense in xLenses)
            {
                var lensModel = xLense.Attribute("model").Value;
                var lensType = xLense.Attribute("type").Value;
                var lensPrice = xLense.Attribute("price");

                var lens = context.Lenses.FirstOrDefault(l => l.Model == lensModel);

                if (lens != null)
                {
                    Console.WriteLine("Existing lens: {0}", lensModel);
                }

                else
                {
                    lens = new Lens
                    {
                        Model = lensModel,
                        Type = lensType,
                        Price = lensPrice != null ? decimal.Parse(lensPrice.Value) : default(decimal?),
                        ManufacturerId = manufacturer.Id
                    };

                    context.Lenses.Add(lens);
                    context.SaveChanges();
                    Console.WriteLine("Created lens: {0}", lensModel);
                }

            }
        }
        static void Main()
        {
            var context = new PhotographySystemEntities();
            var xmlDocument = XDocument.Load("../../manufacturers-and-goods.xml");
            var manufacturers =
                from manufacturer in xmlDocument.Descendants("manufacturer")
                select new
                {
                    Name = manufacturer.Attribute("name").Value,
                    Cameras =
                        from camera in xmlDocument.Descendants("camera")
                        select new
                        {
                            Model = 
                                camera.Attribute("model") == null ?
                                null : 
                                camera.Attribute("model").Value,
                            Year = 
                                camera.Attribute("year") == null ?
                                null :
                                camera.Attribute("year").Value,
                            Price = 
                                camera.Attribute("price") == null ?
                                (decimal?)null :
                                decimal.Parse(camera.Attribute("price").Value),
                            Megapixels =
                                camera.Attribute("megapixels") == null ?
                                (int?)null :
                                int.Parse(camera.Attribute("megapixels").Value)
                        },
                    Lenses =
                        from lens in xmlDocument.Descendants("lens")
                        select new
                        {
                            Model =
                                lens.Attribute("model") == null ?
                                null :
                                lens.Attribute("model").Value,
                            Type =
                                lens.Attribute("type") == null ?
                                null :
                                lens.Attribute("type").Value,
                            Price =
                                lens.Attribute("price") == null ?
                                (decimal?)null :
                                decimal.Parse(lens.Attribute("price").Value)
                        }
                };

            foreach (var manufacturer in manufacturers)
            {
                if (context.Manufacturers.Any(m => m.Name == manufacturer.Name))
                {
                    Console.WriteLine(
                        "Manufacturer {0} already exists.",
                        manufacturer.Name
                    );
                }
                else
                {
                    var newManufacturer = new Manufacturer()
                    {
                        Name = manufacturer.Name
                    };

                    context.Manufacturers.Add(newManufacturer);

                    context.SaveChanges();

                    Console.WriteLine(
                        "Successfully added manufacturer {0}.",
                        manufacturer.Name
                    );

                    using (var transaction = context.Database.BeginTransaction())
                    {
                        try
                        {
                            int cameraYear = 0;

                            foreach (var camera in manufacturer.Cameras)
                            {
                                if (camera.Year != null)
                                {
                                    cameraYear = int.Parse(camera.Year);
                                }

                                context.Cameras.Add(new Camera()
                                {
                                    Model = camera.Model,
                                    Year = cameraYear,
                                    Price = camera.Price,
                                    Megapixels = camera.Megapixels,
                                    ManufacturerId = newManufacturer.Id
                                });
                            }

                            foreach (var lens in manufacturer.Lenses)
                            {
                                context.Lenses.Add(new Lens()
                                {
                                    Model = lens.Model,
                                    Type = lens.Type,
                                    Price = lens.Price,
                                    ManufacturerId = newManufacturer.Id
                                });
                            }

                            context.SaveChanges();
                            transaction.Commit();
                        }
                        catch (DbEntityValidationException)
                        {
                            transaction.Rollback();
                        }
                    }
                }
            }
        }