public UnitOfWork(PhotographyContext context) { //Database.SetInitializer<PhotographyContext>(null); if (context == null) { context = new PhotographyContext(); _context = context; uow = new UnitOfWork(_context); } _context = context; }
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(); } }
private static void ExportedPhotographOrdered(PhotographyContext context) { var orderedPhotographters = context.Photographers .OrderBy(x => x.FirstName) .ThenByDescending(x => x.LastName) .Select(ph => new { ph.FirstName, ph.LastName, ph.Phone }); var orderedAsJson = JsonConvert.SerializeObject(orderedPhotographters, Newtonsoft.Json.Formatting.Indented); File.WriteAllText("..//..//..//ExportedJson//orderedPhotographers.json", orderedAsJson); }
private static void ImportWorkshop(PhotographyContext context) { XDocument xDocument = XDocument.Load(WorkShopPaths); var workshop = xDocument.XPathSelectElements("workshops/workshop"); foreach (var work in workshop) { var name = work.Attribute("name"); var starrtDate = (DateTime)work.Attribute("start-date"); var endDate = (DateTime)work.Attribute("end-date"); var location = work.Attribute("location"); var pricePerParticipant = (decimal)work.Attribute("price"); if (name == null || location == null || starrtDate == null || endDate == null) { Console.WriteLine("Error. Invalid data provided"); continue; } var newWorkshop = new Workshop { Name = name.Value, StartDate = starrtDate, EndDate = endDate, Location = work.Value, PricePerParticipant = pricePerParticipant, }; try { context.Workshops.Add(newWorkshop); } catch (DbEntityValidationException ex) { context.Workshops.Remove(newWorkshop); } Console.WriteLine($"Successfully imported {work.Name}"); } }
private static void ImportAccesorie(XElement accesry, PhotographyContext context, Random rnd) { var name = accesry.Attribute("name"); int accesoriesNumber = rnd.Next(1, context.Accessories.Count() + 1); if (name == null) { Console.WriteLine("Error. Invalid data provided"); } var accespry = new Accessory { Name = name.Value, Owner = context.Photographers.FirstOrDefault(x => x.Id == accesoriesNumber) }; context.Accessories.Add(accespry); Console.WriteLine($"Successfully imported {accespry.Name}"); }
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 ImportLenses(PhotographyContext context) { var jsoon = File.ReadAllText(LensPath); var lensesEntity = JsonConvert.DeserializeObject <IEnumerable <LensDto> >(jsoon); foreach (var lens in lensesEntity) { if (lens.Make == null || lens.CompatibleWith == null) { Console.WriteLine("Error. Invalid data provided"); continue; } var lensEntityAsJson = new Lens { Make = lens.Make, FocalLenght = lens.FocalLength, MaxAperture = lens.MaxAperture, CompatibleWith = lens.CompatibleWith }; try { context.Lenses.Add(lensEntityAsJson); context.SaveChanges(); } catch (DbEntityValidationException ex) { context.Lenses.Remove(lensEntityAsJson); context.SaveChanges(); } Console.WriteLine($"Successfully imported {lens.Make} {lens.FocalLength}mm f{lens.MaxAperture}."); } }
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); } }
private static void ImportPhotographers(PhotographyContext context) { var json = File.ReadAllText(PhotograogersPath); var photographersEntity = JsonConvert.DeserializeObject <IEnumerable <PhotographerDto> >(json); foreach (var photographer in photographersEntity) { if (photographer.FirstName == null || photographer.LastName == null || photographer.Phone == null) { Console.WriteLine("Error. Invalid data provided"); continue; } var newPhotograph = new Photographer { FirstName = photographer.FirstName, LastName = photographer.LastName, Phone = photographer.Phone }; try { context.Photographers.Add(newPhotograph); context.SaveChanges(); } catch (DbEntityValidationException ex) { context.Photographers.Remove(newPhotograph); } Console.WriteLine($"Successfully imported {photographer.FirstName} {photographer.LastName} | Lenses: {photographer.Lenses.Count}"); } context.SaveChanges(); }
public static void Main() { var context = new PhotographyContext(); context.Database.Initialize(true); }
static void Main(string[] args) { var context = new PhotographyContext(); var count = context.Cameras.Count(); }
private static void ImportCameras(PhotographyContext context) { var json = File.ReadAllText(CamerasParh); var camerasEntity = JsonConvert.DeserializeObject <IEnumerable <CameraDto> >(json); foreach (var camera in camerasEntity) { if (camera.Make == null || camera.Model == null || camera.Type == null || camera.MinIso < 100) { Console.WriteLine("Error. Invalid data provided"); continue; } if (camera.Type == "DSLR") { var newdslrCamera = new DslrCamera { Make = camera.Make, Model = camera.Model, MinIso = camera.MinIso, MaxIso = camera.MaxIso, IsFullFrame = camera.IsFullFrame, MaxShutterSpeed = camera.MaxShutterSpeed, }; try { context.Cameras.Add(newdslrCamera); context.SaveChanges(); } catch (DbEntityValidationException db) { context.Cameras.Remove(newdslrCamera); context.SaveChanges(); } Console.WriteLine($"Successfully imported {camera.Type} {camera.Make} {camera.Model}"); } else { var mirrorlesCamera = new MirrorlessCamera() { Make = camera.Make, Model = camera.Model, MinIso = camera.MinIso, MaxIso = camera.MaxIso, IsFullFrame = camera.IsFullFrame, MaxVideoResolution = camera.MaxVideoResolution, MaxFrameRate = camera.MaxFrameRate }; try { context.Cameras.Add(mirrorlesCamera); context.SaveChanges(); } catch (DbEntityValidationException db) { context.Cameras.Remove(mirrorlesCamera); Console.WriteLine($"Successfully imported {camera.Type} {camera.Make} {camera.Model}"); } } context.SaveChanges(); } }
static void Main(string[] args) { var context = new PhotographyContext(); ExportedPhotographOrdered(context); }
static void Main() { IPhotographyContext context = new PhotographyContext(); QueriesExecutor.Create(new UnitOfWork(context)).RunAllQueries(); }