static void Main()
        {
            var context = new PhotographySystemEntities();
            var cameras = context.Cameras.OrderBy(c => c.Manufacturer.Name + c.Model);

            foreach (var camera in cameras)
            {
                Console.WriteLine("{0} {1}",camera.Manufacturer.Name, camera.Model);
            }
        }
        static void Main()
        {
            var context = new PhotographySystemEntities();

            var photographs = context.Photographs
                .OrderBy(p => p.Title)
                .Select(p => new
                {
                    p.Title,
                    Category = p.Category.Name,
                    p.Link,
                    Camera = new {
                        p.Equipment.Camera.Model,
                        p.Equipment.Camera.Megapixels
                    },
                    Lens = new
                    {
                        p.Equipment.Lens.Model,
                        p.Equipment.Lens.Price
                    }
                });

            XElement photographsElement = new XElement("photographs");

            foreach (var photograph in photographs)
            {
                XElement photographElement = new XElement("photograph");
                photographElement.SetAttributeValue("title", photograph.Title);
                XElement categoryElement = new XElement("category", photograph.Category);
                photographElement.Add(categoryElement);
                XElement linkElement = new XElement("link", photograph.Link);
                photographElement.Add(linkElement);

                XElement equipmentElement = new XElement("equipment");

                XElement cameraElement = new XElement("camera", photograph.Camera.Model);
                cameraElement.SetAttributeValue("megapixels", photograph.Camera.Megapixels);
                equipmentElement.Add(cameraElement);

                XElement lensElement = new XElement("lens", photograph.Lens.Model);
                if (photograph.Lens.Price != null)
                {
                    lensElement.SetAttributeValue("price", String.Format("{0:0.00}", photograph.Lens.Price));
                }
                equipmentElement.Add(lensElement);
                photographElement.Add(equipmentElement);
                photographsElement.Add(photographElement);
            }

            var resultXmlDoc = new XDocument();
            resultXmlDoc.Add(photographsElement);
            resultXmlDoc.Save("../../photographs.xml");
        }
 private static void AddLensToManufacturer(PhotographySystemEntities context, Manufacturer manufacturer, Lens lense)
 {
     if (manufacturer != null)
     {
         if (manufacturer.Lenses.Contains(lense))
         {
             Console.WriteLine("Existing lens: {0}", lense.Model);
         }
         else
         {
             manufacturer.Lenses.Add(lense);
             context.SaveChanges();
             Console.WriteLine("Created lens: {0}", lense.Model);
         }
     }
 }
        private static void CreateNewLensesIfNotExists(PhotographySystemEntities context, Manufacturer manufacturer, IEnumerable<XElement> xLenses)
        {
            foreach (var xLense in xLenses)
            {
                var lenseModel = xLense.Attribute("model").Value;
                var lenseType = xLense.Attribute("type").Value;
                var xLensePrice = xLense.Attribute("price");
                string lensePrice = null;
                if (xLensePrice != null)
                {
                    lensePrice = xLensePrice.Value;
                }

                var lense = context.Lenses
                    .FirstOrDefault(l => l.Model == lenseModel);

                if (lense != null)
                {
                    Console.WriteLine("Existing lens: {0}", lenseModel);
                }
                else
                {
                    lense = new Lens()
                    {
                        Model = lenseModel,
                        Type = lenseType
                    };

                    if (lensePrice != null)
                    {
                        lense.Price = decimal.Parse(lensePrice);
                    }

                    context.Lenses.Add(lense);
                }

                AddLensToManufacturer(context, manufacturer, lense);
            }
        }
        static void Main()
        {
            var context = new PhotographySystemEntities();
            JavaScriptSerializer js = new JavaScriptSerializer();

            var manufacturers = context.Manufacturers
                .OrderBy(m => m.Name)
                .Select(m => new
                {
                    manufacturer = m.Name,
                    cameras = m.Cameras
                        .OrderBy(c => c.Model)
                        .Select(c => new
                        {
                            model = c.Model,
                            price = c.Price
                        })
                });

            var json = js.Serialize(manufacturers);
            System.IO.File.WriteAllText(@"..\..\manufactureres-and-cameras.json", json);
        }
        private static Manufacturer CreateNewManufacturerIfNotExists(PhotographySystemEntities context, XElement xManufacturer)
        {
            Manufacturer manufacturer = null;
            var xManufacturerName = xManufacturer.Element("manufacturer-name");
            if (xManufacturerName != null)
            {
                string manufacturerName = xManufacturerName.Value;
                manufacturer = context.Manufacturers.FirstOrDefault(m => m.Name == manufacturerName);
                if (manufacturer != null)
                {
                    Console.WriteLine("Existing manufacturer: {0}", manufacturerName);
                }
                else
                {
                    manufacturer = new Manufacturer(){ Name = manufacturerName};
                    context.Manufacturers.Add(manufacturer);
                    context.SaveChanges();
                    Console.WriteLine("Created manufacturer: {0}", manufacturerName);
                }
            }

            return manufacturer;
        }
        static void Main()
        {
            var context = new PhotographySystemEntities();

            XDocument xmlDocument = XDocument.Load("../../manufacturers-and-lenses.xml");
            var xManufacturers = xmlDocument.XPathSelectElements("/manufacturers-and-lenses/manufacturer");
            int count = 0;

            foreach (var xManufacturer in xManufacturers)
            {
                Console.WriteLine("Processing manufacturer #{0} ...", ++count);
                Manufacturer manufacturer = CreateNewManufacturerIfNotExists(context, xManufacturer);
                var xLenses = xManufacturer.XPathSelectElements("lenses/lens");
                CreateNewLensesIfNotExists(context, manufacturer, xLenses);
                Console.WriteLine();
            }
        }