コード例 #1
2
ファイル: JsonParser.cs プロジェクト: Elinos/TelerikAcademy
        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!");
        }
コード例 #2
0
        private static City GetCity(string cityName)
        {
            var city = db.Cities.FirstOrDefault(c => c.Name == cityName);
            if (city == null)
            {
                city = new City
                {
                    Name = cityName
                };
            }

            return city;
        }
コード例 #3
0
        private City GetOrCreateCity(string name)
        {
            City city;
            this.cities.TryGetValue(name, out city);

            if (city == null)
            {
                city = new City()
                {
                    Name = name
                };

                this.cities[name] = city;
            }

            return city;
        }
コード例 #4
0
ファイル: JsonImporter.cs プロジェクト: AYankova/Databases
        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;
        }