Beispiel #1
0
 private void PopulateTablesWithMakeModelOptions()
 {
     using (var db = new Models.InsuranceQuoteDBEntities())
     {
         var rows = db.Year_Make_Model_Trim_Full_Specs_by_Teoalida_SAMPLE.ToList();
         foreach (var row in rows)
         {
             //WARNING: in this particular data set, row.make, row.model. row.year, and row.trim are never empty
             //consider this if using this routine to ever import another list
             Models.AutoMake   autoMake;
             Models.AutoModel  autoModel;
             Models.AutoOption autoOption;
             var makes2 = db.AutoMakes.Where(x => x.MakeName == row.Make).OrderBy(x => x.Id).ToList();
             if (makes2.Count() != 0)
             {
                 autoMake = makes2.Last();
             }
             else
             {
                 autoMake = new Models.AutoMake {
                     MakeName = row.Make
                 };
                 db.AutoMakes.Add(autoMake);
                 _ = db.SaveChanges(); //saving sets the autoincemented ID value
             }
             var models2 = db.AutoModels.Where(x => x.AutoMakeID == autoMake.Id && x.ModelName == row.Model && x.ModelYear == row.Year).OrderBy(x => x.Id).ToList();
             if (models2.Count() != 0)
             {
                 autoModel = models2.Last();
             }
             else
             {
                 autoModel = new Models.AutoModel
                 {
                     AutoMakeID = autoMake.Id,
                     ModelName  = row.Model,
                     ModelYear  = row.Year
                 };
                 db.AutoModels.Add(autoModel);
                 _ = db.SaveChanges(); //saving sets the autoincemented ID value
             }
             var options2 = db.AutoOptions.Where(x => x.AutoModelId == autoModel.Id && x.OptionDescription == row.Trim).OrderBy(x => x.Id).ToList();
             if (options2.Count() == 0)
             {
                 autoOption = new Models.AutoOption
                 {
                     OptionDescription = row.Trim,
                     AutoModelId       = autoModel.Id,
                     CurbWeightLbs     = row.Curb_weight_lbs,
                     Horsepower        = row.Horsepower_HP,
                     PriceDollars      = row.Price
                 };
                 db.AutoOptions.Add(autoOption);
                 _ = db.SaveChanges();
             }
         }
     }
 }