private static Manufacturer CreateManufacturerIfNotExists(PhotographyContext context, XElement xManufacturer)
        {
            Manufacturer manufacturer = null;
            var xElementManufacturerName = xManufacturer.Element("manufacturer-name");
            if (xElementManufacturerName != null)
            {
                var manufacturerName = xElementManufacturerName.Value;
                manufacturer = context.Manufacturers.FirstOrDefault(x => x.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;
        }
        private static void CreateLensesIfNotExist(PhotographyContext context, IEnumerable<XElement> xLenses, Manufacturer manufacturer)
        {
            foreach (var xLens in xLenses)
            {
                var lensModel = xLens.Attribute("model").Value;
                var lensType = xLens.Attribute("type").Value;
                var lensPrice = xLens.Attribute("price");

                var lens = context.Lenses.FirstOrDefault(x => x.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 PhotographyContext();
     var cameras = context.Cameras
         .Select(x => x.Manufacturer.Name + " " + x.Model)
         .OrderBy(x => x);
     Console.WriteLine(string.Join("\n", cameras));
 }
        static void Main()
        {
            var context = new PhotographyContext();

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

            var resultXml = new XElement("photographs");
            foreach (var photograph in photographs)
            {
                var photographXml = new XElement("photograph");
                photographXml.Add(new XAttribute("title", photograph.Title));
                photographXml.Add(new XElement("category", photograph.Category));
                photographXml.Add(new XElement("link", photograph.Link));
                var equipmentXml = new XElement("equipment");
                equipmentXml.Add(new XElement("camera", photograph.Equipment.Camera.Name, new XAttribute("megapixels", photograph.Equipment.Camera.Megapixels)));

                if (photograph.Equipment.Lens.Price.HasValue)
                {
                    equipmentXml.Add(new XElement("lens", photograph.Equipment.Lens.Name, new XAttribute("price", string.Format("{0:f2}", photograph.Equipment.Lens.Price))));
                }
                else
                {
                    equipmentXml.Add(new XElement("lens", photograph.Equipment.Lens.Name));
                }

                photographXml.Add(equipmentXml);

                resultXml.Add(photographXml);
            }

            var resultXmlDoc = new XDocument();
            resultXmlDoc.Add(resultXml);
            resultXmlDoc.Save("photographs.xml");

            Console.WriteLine("Photographs exported to photographs.xml");
        }
 static void Main()
 {
     var inputXml = XDocument.Load("../../manufacturers-and-lenses.xml");
     var xManufacturers = inputXml.XPathSelectElements("/manufacturers-and-lenses/manufacturer");
     var context = new PhotographyContext();
     var manufacturersCount = 0;
     foreach (var xManufacturer in xManufacturers)
     {
         Console.WriteLine("Processing manufacturer #{0} ...", ++manufacturersCount);
         Manufacturer manufacturer = CreateManufacturerIfNotExists(context, xManufacturer);
         var xLenses = xManufacturer.XPathSelectElements("lenses/lens");
         CreateLensesIfNotExist(context, xLenses, manufacturer);
         Console.WriteLine();
     }
 }
 static void Main()
 {
     var context = new PhotographyContext();
     var manufacturersWithTeams = context.Manufacturers
         .OrderBy(x => x.Name)
         .Select(x => new
         {
             manufacturer = x.Name,
             cameras = x.Cameras
                 .OrderBy(c => c.Model)
                 .Select(c => new
                 {
                     model = c.Model,
                     price = c.Price
                 })
         });
     var jsSerializer = new JavaScriptSerializer();
     var json = jsSerializer.Serialize(manufacturersWithTeams);
     File.WriteAllText("manufacturers-and-cameras.json", json);
     Console.WriteLine("File manufacturers-and-cameras.json exported.");
 }
        private static void ProcessRequest(EquipmentRequest request)
        {
            var context = new PhotographyContext();
            var manufacturer = context.Manufacturers.FirstOrDefault(x => x.Name == request.Manufacturer);

            var cameraIds = context.Cameras
                .Where(x => x.ManufacturerId == manufacturer.Id)
                .Select(x => x.Id)
                .ToList();
            var lensIds = context.Lenses
                .Where(x => x.ManufacturerId == manufacturer.Id)
                .Select(x => x.Id)
                .ToList();
            var rnd = new Random();
            for (int i = 0; i < request.GenerateCount; i++)
            {
                var equipment = new Equipment();
                var lensRand = rnd.Next(lensIds.Count);
                var cameraRand = rnd.Next(cameraIds.Count);
                equipment.LensId = lensIds[lensRand];
                equipment.CameraId = cameraIds[cameraRand];

                context.Equipments.Add(equipment);
                context.SaveChanges();

                var equipmentDb = context.Equipments
                    .Include(x => x.Camera)
                    .Include(x => x.Lens)
                    .FirstOrDefault(x => x.Id == equipment.Id);

                Console.WriteLine("Equipment added: {0} (Camera: {1} - Lens: {2})",
                    manufacturer.Name,
                    equipmentDb.Camera.Model,
                    equipmentDb.Lens.Model);
            }
        }