public void AddData(string directoryName, CarEntities db) { var jsonFilesContent = GetAllJsonFilesContent(directoryName); var convertedFilesContent = ConvertJsonFileContentToJsonModels(jsonFilesContent); Console.WriteLine("Start adding json data to database"); AddDataToDatabase(convertedFilesContent, db); Console.WriteLine("Finsihed!"); }
public static void Main() { //NOTE!!! I use SQL server. If you use SQLEXPRESS, please, change the connection string in all App.config files in the solution. var db = new CarEntities(); db.Configuration.AutoDetectChangesEnabled = false; //NOTE!!! This is extremely slow operation to run over all of the json files. //The json files are in the debug folder of the ConsoleClient. var importer = new JsonImporter(); importer.AddData("JsonData", db); }
private static void AddDataToDatabase(List<List<JsonCar>> convertedFilesContent, CarEntities db) { int counter = 0; foreach (var fileContent in convertedFilesContent) { foreach (var car in fileContent) { var model = car.Model; var tranmissionType = car.TransmissionType; var year = car.Year; var price = car.Price; Manufacturer carMaker =db.Manufacturers.FirstOrDefault(manufacturer => manufacturer.Name.Equals(car.ManufacturerName)); if (carMaker == null) { carMaker = new Manufacturer() { Name = car.ManufacturerName }; } City dealerCity = db.Cities.FirstOrDefault(city => city.Name == car.Dealer.City); if (dealerCity == null) { dealerCity = new City() { Name = car.Dealer.City }; } var carDealer = new Dealer() { Name = car.Dealer.Name, Cities = new HashSet<City>()}; carDealer.Cities.Add(dealerCity); var newCar = new Car() { Model = model, Year = year, TransmissionType = tranmissionType, Price = price, Dealer = carDealer, Manufacturer = carMaker }; db.Cars.Add(newCar); db.SaveChanges(); counter++; if (counter % 100 == 0) { Console.Write("."); } } } db.SaveChanges(); }