public void ParseAllFiles() { Console.WriteLine("Importing cars from JSON"); db.Configuration.AutoDetectChangesEnabled = false; for (int i = 0; i < 5; i++) { Console.WriteLine("From file No {0}", i + 1); currentListOfCars = this.LoadFile(rootPath + "data." + i + ".json"); var index = 0; foreach (var car in currentListOfCars) { var currentCarToImport = new Car { Year = car.Year, TransmissionType = (TransmissionType)car.TransmissionType, Model = car.Model, Price = car.Price }; var manufacturer = this.db.Manufacturers.FirstOrDefault(m => m.Name == car.ManufacturerName); if (manufacturer == null) { manufacturer = new Manufacturer { Name = car.ManufacturerName }; } currentCarToImport.Manufacturer = manufacturer; var dealer = this.db.Dealers.FirstOrDefault(d => d.Name == car.Dealer.Name); if (dealer == null) { dealer = new Dealer { Name = car.Dealer.Name }; } var city = this.db.Cities.FirstOrDefault(c => c.Name == car.Dealer.City); if (city == null) { city = new City { Name = car.Dealer.City }; } dealer.Cities.Add(city); currentCarToImport.Dealer = dealer; db.Cars.Add(currentCarToImport); db.SaveChanges(); index++; if (index % 100 == 0) { Console.Write("."); } } db.SaveChanges(); Console.WriteLine("Importing from file No {0} finished!", i + 1); } db.Configuration.AutoDetectChangesEnabled = true; Console.WriteLine("Importing cars from JSON finished!"); }
static void Main() { // Working! var db = new CarsDbContext(); db.Configuration.AutoDetectChangesEnabled = false; for (int i = 0; i <= 4; i++) { var filePath = "../../../../JSONData/data." + i + ".json"; var jsonText = File.ReadAllText(filePath); JArray allCars = JArray.Parse(jsonText); Console.WriteLine("Adding cars from file: {0}", filePath); int counter = 0; foreach (var car in allCars) { Manufacturer newManufacturer = new Manufacturer { Name = car["ManufacturerName"].ToString() }; Dealer newDealer = new Dealer { Name = car["Dealer"]["Name"].ToString() }; newDealer.Cities.Add(new City { Name = car["Dealer"]["City"].ToString() }); Car newCar = new Car { Model = car["Model"].ToString(), TransmisionType = int.Parse(car["TransmissionType"].ToString()), Price = decimal.Parse(car["Price"].ToString()), Year = int.Parse(car["Year"].ToString()), Manufacturer = newManufacturer, Dealer = newDealer }; newManufacturer.Cars.Add(newCar); newDealer.Cars.Add(newCar); db.Manufacturers.Add(newManufacturer); db.Dealers.Add(newDealer); db.Cars.Add(newCar); Console.Write("."); if (counter % 100 == 0) { db.SaveChanges(); } counter++; } db.SaveChanges(); Console.WriteLine("\nFile Read Complete -> All Cars where added successfuly!"); Console.WriteLine("\n"); } db.Configuration.AutoDetectChangesEnabled = false; }
private Manufacturer GetOrCreateManifacturer(string manufacturerName) { var manufacturer = _carsContext.Manufacturers.SingleOrDefault(m => m.Name == manufacturerName); if (manufacturer==null) { manufacturer = new Manufacturer { Name = manufacturerName }; } return manufacturer; }
private Manufacturer GetOrCreateManufacturer(string name) { Manufacturer manufacturer; this.manufacturers.TryGetValue(name, out manufacturer); if (manufacturer == null) { manufacturer = new Manufacturer() { Name = name }; this.manufacturers[name] = manufacturer; } return manufacturer; }
private static Manufacturer GetManufacturer(string manufacturerName) { var manufacturer = db.Manufacturers.FirstOrDefault(m => m.Name == manufacturerName); if (manufacturer == null) { manufacturer = new Manufacturer { Name = manufacturerName }; } return manufacturer; }
public static void Import() { var carsToAdd = Directory .GetFiles(Directory.GetCurrentDirectory() + "/JsonFiles/") .Where(f => f.EndsWith("json")) .Select(f => File.ReadAllText(f)) .SelectMany(str => JsonConvert.DeserializeObject<IEnumerable<JsonCar>>(str)) .ToList(); var addedCities = new HashSet<string>(); var addedManufacturers = new HashSet<string>(); var addedCars = 0; var db = new CarsDbContext(); db.Configuration.AutoDetectChangesEnabled = false; Console.WriteLine("Adding cars"); foreach (var car in carsToAdd) { var cityName = car.Dealer.City; if (!addedCities.Contains(car.Dealer.City)) { var city = new City { Name = cityName }; db.Cities.Add(city); db.SaveChanges(); addedCities.Add(cityName); } var manufacturer = car.ManufacturerName; if (!addedManufacturers.Contains(car.ManufacturerName)) { var newManufacturer = new Manufacturer { Name = manufacturer }; addedManufacturers.Add(manufacturer); db.Manufacturers.Add(newManufacturer); db.SaveChanges(); } var dealerToAdd = new Cars.Models.Dealer { Name = car.Dealer.Name }; var dbCity = db.Cities .FirstOrDefault(c => c.Name == cityName); dealerToAdd.Cities.Add(dbCity); var dbManufacturer = db.Manufacturers .FirstOrDefault(m => m.Name == car.ManufacturerName); var carToAdd = new Car { Manufacturer = dbManufacturer, Dealer = dealerToAdd, Model = car.Model, Price = car.Price, TransmissionType = car.TransmissionType, Year = (short)car.Year }; db.Cars.Add(carToAdd); if (addedCars % 100 == 0) { Console.Write("."); db.SaveChanges(); db.Dispose(); db = new CarsDbContext(); db.Configuration.AutoDetectChangesEnabled = false; } addedCars++; } db.SaveChanges(); db.Configuration.AutoDetectChangesEnabled = true; }