コード例 #1
0
        public static void ImportData()
        {
            var currentDirectory = Directory.GetCurrentDirectory();
            var cars = Directory
                .GetFiles(currentDirectory + "/Data.Json.Files/")
                .Where(f => f.EndsWith(".json"))
                .Select(f => File.ReadAllText(f))
                .SelectMany(str => JsonConvert.DeserializeObject<IEnumerable<CarJson>>(str))
                .ToList();

            var db = new CarsDbContext();
            db.Configuration.ValidateOnSaveEnabled = false;
            db.Configuration.AutoDetectChangesEnabled = false;

            var cities = new HashSet<string>();
            var manufacturers = new HashSet<string>();

            Console.Write("Adding cars");

            var carCount = 0;
            foreach (var car in cars)
            {
                var currentCityName = car.Dealer.City;

                if (!cities.Contains(currentCityName))
                {
                    var currentCity = new City
                    {
                        Name = currentCityName
                    };

                    cities.Add(currentCityName);
                    db.Cities.Add(currentCity);
                    db.SaveChanges();
                }

                var currentDealerName = car.Dealer.Name;
                var currentDealer = new Dealer
                {
                    Name = currentDealerName
                };

                var dbCity = db.Cities.FirstOrDefault(c => c.Name == currentCityName);
                currentDealer.Cities.Add(dbCity);

                var currentManufacturerName = car.Manufacturer;
                if (!manufacturers.Contains(currentManufacturerName))
                {
                    var currentManufacturer = new Manufacturer
                    {
                        Name = currentManufacturerName
                    };

                    manufacturers.Add(currentManufacturerName);
                    db.Manufacturers.Add(currentManufacturer);
                    db.SaveChanges();
                }

                db.SaveChanges();

                var dbManufacturer = db.Manufacturers.FirstOrDefault(m => m.Name == car.Manufacturer);

                var currentCar = new Car
                {
                    Year = car.Year,
                    Transmission = car.TransmissionType,
                    Model = car.Model,
                    Price = car.Price,
                    Manufacturer = dbManufacturer,
                    Dealer = currentDealer
                };

                db.Cars.Add(currentCar);

                if (carCount % 100 == 0)
                {
                    Console.Write(".");
                    db.SaveChanges();
                    db.Dispose();
                    db = new CarsDbContext();
                }

                carCount++;
            }

            db.SaveChanges();

            db.Configuration.ValidateOnSaveEnabled = true;
            db.Configuration.AutoDetectChangesEnabled = true;
        }
コード例 #2
0
        public static void ImportData(string directory)
        {
            var db = new CarsDbContext();
            db.Configuration.ValidateOnSaveEnabled = false;
            db.Configuration.AutoDetectChangesEnabled = false;

            using (StreamReader reader = new StreamReader(directory))
            {
                var json = reader.ReadToEnd();
                //Console.WriteLine(json);

                var cars = JsonConvert.DeserializeObject<List<CarJson>>(json);

                //StringComparer.OrdinalIgnoreCase in constructor
                //new HashSet<string>
                var dealers = new List<DealerJson>();
                var cities = new HashSet<string>();
                var manufacturers = new HashSet<string>();

                foreach (var car in cars)
                {
                    cities.Add(car.Dealer.City);
                    dealers.Add(car.Dealer);
                    manufacturers.Add(car.Manufacturer);
                }

                foreach (var city in cities)
                {
                    // with 1 dealer in two towns it will make two of the same dealers
                    db.Cities.AddOrUpdate(new City
                    {
                        Name = city,
                        Dealers = dealers.Where(d => d.City == city).Select(d => new Dealer
                        {
                            Name = d.Name
                        }).ToList()
                    });
                }

                db.SaveChanges();

                foreach (var manufacturer in manufacturers)
                {
                    db.Manufacturers.AddOrUpdate(new Manufacturer
                    {
                        Name = manufacturer
                    });
                }

                db.SaveChanges();

                for (var car = 0; car < cars.Count; car++)
                {
                    // slow ?
                    var currentCarDealersName = cars[car].Dealer.Name;
                    var currentDealer = db.Dealers.Where(d => d.Name == currentCarDealersName).First();

                    var currentCarManufacturersName = cars[car].Manufacturer;
                    var currentManufacturer = db.Manufacturers.Where(m => m.Name == currentCarManufacturersName).First();

                    db.Cars.AddOrUpdate(new Car
                    {
                        Year = cars[car].Year,
                        Transmission = cars[car].TransmissionType,
                        Model = cars[car].Model,
                        Price = cars[car].Price,
                        Manufacturer = currentManufacturer,
                        Dealer = currentDealer
                    });

                    if (car % 100 == 0)
                    {
                        db.SaveChanges();
                        db.Dispose();
                        db = new CarsDbContext();
                    }

                    db.SaveChanges();

                    db.Configuration.ValidateOnSaveEnabled = true;
                    db.Configuration.AutoDetectChangesEnabled = true;
                }
            }
        }