private static void ProcessLenses(IEnumerable<XElement> lenses, Manufacturer manu, PhotographySystemEntities context) { foreach (var lens in lenses) { var lensModel = lens.Attribute("model").Value; var lensType = lens.Attribute("type").Value; var xlensPrice = lens.Attribute("price"); string lensPrice = null; if (xlensPrice != null) { lensPrice = xlensPrice.Value; } var newLens = context.Lenses.FirstOrDefault(l => l.Model == lensModel); if (newLens == null) { newLens = new Lens {Model = lensModel, Type = lensType}; if (lensPrice != null) { newLens.Price = decimal.Parse(lensPrice); } context.Lenses.Add(newLens); manu.Lenses.Add(newLens); context.SaveChanges(); Console.WriteLine("Created lens: {0}", lensModel); } else { Console.WriteLine("Existing lens: {0}", lensModel); } } }
private static Manufacturer ProcessManufacturer(XElement manufacturer, PhotographySystemEntities context) { var manuName = manufacturer.Element("manufacturer-name").Value; var manu = context.Manufacturers.FirstOrDefault(m => m.Name == manuName); if (manu == null) { manu = new Manufacturer {Name = manuName}; context.Manufacturers.Add(manu); context.SaveChanges(); Console.WriteLine("Created manufacturer: {0}", manuName); } else { Console.WriteLine("Existing manufacturer: {0}", manuName); } return manu; }
private static void GenRndEquip(XmlSpec xmlSpec, PhotographySystemEntities context) { Random rnd = new Random(); for (int i = 0; i < xmlSpec.Count; i++) { var equipment = new Equipment(); var lenses = context.Lenses.Where(l => l.Manufacturer.Name == xmlSpec.ManuName).Select(l => l).ToList(); var cameras = context.Cameras.Where(c => c.Manufacturer.Name == xmlSpec.ManuName).Select(c => c).ToList(); equipment.Lens = lenses.ElementAt(rnd.Next(lenses.Count())); equipment.Camera = cameras.ElementAt(rnd.Next(cameras.Count())); context.Equipments.Add(equipment); context.SaveChanges(); Console.WriteLine("Equipment added: {0} (Camera: {1} - Lens: {2})", xmlSpec.ManuName, equipment.Camera.Model, equipment.Lens.Model); } }