public void Seed() { //If the database is not present or if migrations are required //create the database and/or run the migrations _context.Database.Migrate(); var staticExtRepo = new StaticDataExtRepository(); var currentVersion = _sd.GetCurrentStaticDataVersion(); if (currentVersion.Id == 1 && string.IsNullOrEmpty(currentVersion.CreatedBy)) { currentVersion.Comments = "Initial version migrated from Legacy StaticData.json file"; currentVersion.CreatedBy = "Agri.Data.AgriSeeder.cs"; } //AmoniaRetention if (!_context.AmmoniaRetentions.Any()) { var ammoniaRetentions = staticExtRepo.GetAmmoniaRetentions(); _context.AmmoniaRetentions.AddRange(ammoniaRetentions); } //Animal //AnimalSubType if (!_context.Animals.Any()) { var animals = staticExtRepo.GetAnimals(); var animalSubtypes = staticExtRepo.GetAnimalSubTypes(); foreach (var animal in animals) { animal.SetVersion(currentVersion); var subtypes = animalSubtypes.Where(s => s.AnimalId == animal.Id).ToList(); if (subtypes.Any()) { subtypes.Select(st => { st.SetVersion(currentVersion); return(st); }).ToList(); animal.AnimalSubTypes.AddRange(subtypes); } } _context.Animals.AddRange(animals); } //Browser if (!_context.Browsers.Any()) { var browsers = staticExtRepo.GetAllowableBrowsers(); _context.Browsers.AddRange(browsers); } //ConversionFactor if (!_context.ConversionFactors.Any()) { var cFactor = staticExtRepo.GetConversionFactor(); cFactor.SoilTestPPMToPoundPerAcre = staticExtRepo.GetSoilTestNitratePPMToPoundPerAcreConversionFactor(); _context.ConversionFactors.Add(cFactor); } //Default Soil Test if (!_context.DefaultSoilTests.Any()) { var test = staticExtRepo.GetDefaultSoilTest(); test.DefaultSoilTestMethodId = staticExtRepo.GetDefaultSoilTestMethod(); _context.DefaultSoilTests.Add(test); } //Soil Test Ranges if (!_context.PhosphorusSoilTestRanges.Any()) { var ranges = staticExtRepo.GetPhosphorusSoilTestRanges(); _context.PhosphorusSoilTestRanges.AddRange(ranges); } if (!_context.PotassiumSoilTestRanges.Any()) { var ranges = staticExtRepo.GetPotassiumSoilTestRanges(); _context.PotassiumSoilTestRanges.AddRange(ranges); } //Location if (!_context.Locations.Any()) { var locations = staticExtRepo.GetLocations(); _context.Locations.AddRange(locations); } //Regions if (!_context.Regions.Any()) { var regions = staticExtRepo.GetRegions(); _context.Regions.AddRange(regions); } //CropTypes if (!_context.CropTypes.Any()) { var types = staticExtRepo.GetCropTypes(); _context.CropTypes.AddRange(types); } //PrevManureApplicationYear if (!_context.PrevManureApplicationYears.Any()) { var years = staticExtRepo.GetPrevManureApplicationInPrevYears(); _context.PrevManureApplicationYears.AddRange(years); } //PrevYearManureApplicationlDefaultNitrogen if (!_context.PrevYearManureApplicationNitrogenDefaults.Any()) { var nitrogenDefaults = staticExtRepo.GetPrevYearManureNitrogenCreditDefaults(); nitrogenDefaults.Select(nd => { nd.PreviousYearManureAplicationFrequency = nd.FieldManureApplicationHistory.ToString(); nd.SetVersion(currentVersion); return(nd); }).ToList(); _context.PrevYearManureApplicationNitrogenDefaults.AddRange(nitrogenDefaults); } //Crops //CropStkRegion //CropStpRegion //CropYield //PrevCropType if (!_context.Crops.Any()) { var crops = staticExtRepo.GetCrops(); var stksRegions = staticExtRepo.GetCropSoilTestPotassiumRegions(); var stpsRegions = staticExtRepo.GetCropSoilTestPhosphorousRegions(); var cropYields = staticExtRepo.GetCropYields(); var prevCropTypes = staticExtRepo.GetPreviousCropTypes(); foreach (var crop in crops) { crop.SetVersion(currentVersion); if (stksRegions.Any(s => s.CropId == crop.Id)) { crop.CropSoilTestPotassiumRegions.AddRange(stksRegions.Where(s => s.CropId == crop.Id)); } if (stpsRegions.Any(s => s.CropId == crop.Id)) { crop.CropSoilTestPhosphorousRegions.AddRange(stpsRegions.Where(s => s.CropId == crop.Id)); } if (cropYields.Any(s => s.CropId == crop.Id)) { crop.CropYields.AddRange(cropYields.Where(s => s.CropId == crop.Id)); } if (prevCropTypes.Any(c => c.PreviousCropCode == crop.PreviousCropCode)) { crop.PreviousCropTypes.AddRange(prevCropTypes.Where(c => c.PreviousCropCode == crop.PreviousCropCode)); } } currentVersion.Crops.AddRange(crops); _context.Crops.AddRange(crops); } //DensityType if (!_context.DensityUnits.Any()) { var units = staticExtRepo.GetDensityUnits(); _context.DensityUnits.AddRange(units); } //Fertilizer if (!_context.Fertilizers.Any()) { var fertilizers = staticExtRepo.GetFertilizers(); _context.Fertilizers.AddRange(fertilizers); } //FertilizerType if (!_context.FertilizerTypes.Any()) { var types = staticExtRepo.GetFertilizerTypes(); _context.FertilizerTypes.AddRange(types); } //FertilizerUnit if (!_context.FertilizerUnits.Any()) { var units = staticExtRepo.GetFertilizerUnits(); _context.FertilizerUnits.AddRange(units); } //LiquidFertilizerDensity if (!_context.LiquidFertilizerDensities.Any()) { var densities = staticExtRepo.GetLiquidFertilizerDensities(); _context.LiquidFertilizerDensities.AddRange(densities); } //DryMatter if (!_context.DryMatters.Any()) { var dms = staticExtRepo.GetDryMatters(); _context.DryMatters.AddRange(dms); } //NMineralization if (!_context.NitrogenMineralizations.Any()) { var minerals = staticExtRepo.GetNitrogeMineralizations(); _context.NitrogenMineralizations.AddRange(minerals); } //Manure if (!_context.Manures.Any()) { var manures = staticExtRepo.GetManures(); _context.Manures.AddRange(manures); } ////ManureMaterialTypes //if (!_context.ManureMaterialTypes.Any()) //{ // var types = staticDataRepo.GetManureMaterialTypes(); // _context.ManureMaterialTypes.AddRange(types); //} //STKKelownaRange //STKRecommendations if (!_context.SoilTestPotassiumKelownaRanges.Any()) { var ranges = staticExtRepo.GetSoilTestPotassiumKelownaRanges(); var stks = staticExtRepo.GetSoilTestPotassiumRecommendations(); foreach (var stkKelownaRange in ranges) { if (stks.Any(s => s.SoilTestPotassiumKelownaRangeId == stkKelownaRange.Id)) { stkKelownaRange.SoilTestPotassiumRecommendations.AddRange(stks.Where(s => s.SoilTestPotassiumKelownaRangeId == stkKelownaRange.Id)); } } _context.SoilTestPotassiumKelownaRanges.AddRange(ranges); } //STPKelownaRange //STPRecommendations if (!_context.SoilTestPhosphorousKelownaRanges.Any()) { var ranges = staticExtRepo.GetSoilTestPhosphorousKelownaRanges(); var stps = staticExtRepo.GetSoilTestPhosphorousRecommendations(); foreach (var stpKelownaRange in ranges) { if (stps.Any(s => s.SoilTestPhosphorousKelownaRangeId == stpKelownaRange.Id)) { stpKelownaRange.SoilTestPhosphorousRecommendations.AddRange(stps.Where(s => s.SoilTestPhosphorousKelownaRangeId == stpKelownaRange.Id)); } } _context.SoilTestPhosphorousKelownaRanges.AddRange(ranges); } if (!_context.FertilizerMethods.Any()) { var fertilizerMethods = staticExtRepo.GetFertilizerMethods(); _context.FertilizerMethods.AddRange(fertilizerMethods); } if (!_context.UserPrompts.Any()) { var userPrompts = staticExtRepo.GetUserPrompts(); _context.UserPrompts.AddRange(userPrompts); } if (!_context.ExternalLinks.Any()) { var externalLinks = staticExtRepo.GetExternalLinks(); _context.ExternalLinks.AddRange(externalLinks); } if (!_context.Units.Any()) { var units = staticExtRepo.GetUnits(); _context.Units.AddRange(units); } if (!_context.SoilTestMethods.Any()) { var soilTestMethods = staticExtRepo.GetSoilTestMethods(); _context.SoilTestMethods.AddRange(soilTestMethods); } if (!_context.SoilTestPhosphorusRanges.Any()) { var getSoilTestPhosphorusRanges = staticExtRepo.GetSoilTestPhosphorusRanges(); _context.SoilTestPhosphorusRanges.AddRange(getSoilTestPhosphorusRanges); } if (!_context.SoilTestPotassiumRanges.Any()) { var getSoilTestPotassiumRanges = staticExtRepo.GetSoilTestPotassiumRanges(); _context.SoilTestPotassiumRanges.AddRange(getSoilTestPotassiumRanges); } if (!_context.Messages.Any()) { var getMessages = staticExtRepo.GetMessages(); _context.Messages.AddRange(getMessages); } if (!_context.SeasonApplications.Any()) { var getSeasonApplications = staticExtRepo.GetSeasonApplications(); _context.SeasonApplications.AddRange(getSeasonApplications); } if (!_context.Yields.Any()) { var getYields = staticExtRepo.GetYields(); _context.Yields.AddRange(getYields); } if (!_context.NitrogenRecommendations.Any()) { var getNitrogenRecommendations = staticExtRepo.GetNitrogenRecommendations(); _context.NitrogenRecommendations.AddRange(getNitrogenRecommendations); } if (!_context.RptCompletedManureRequiredStdUnits.Any()) { var rptCompletedManureRequiredStdUnits = staticExtRepo.GetRptCompletedManureRequiredStdUnit(); _context.RptCompletedManureRequiredStdUnits.AddRange(rptCompletedManureRequiredStdUnits); } if (!_context.RptCompletedFertilizerRequiredStdUnits.Any()) { var rptCompletedManureRequiredStdUnits = staticExtRepo.GetRptCompletedFertilizerRequiredStdUnit(); _context.RptCompletedFertilizerRequiredStdUnits.AddRange(rptCompletedManureRequiredStdUnits); } //HarvestUnit if (!_context.HarvestUnits.Any()) { var units = staticExtRepo.GetHarvestUnits(); _context.HarvestUnits.AddRange(units); } if (!_context.BCSampleDateForNitrateCredit.Any()) { var bcSampleDateForNitrateCredit = staticExtRepo.GetBCSampleDateForNitrateCredit(); _context.BCSampleDateForNitrateCredit.AddRange(bcSampleDateForNitrateCredit); } //Nutrient Icons if (!_context.NutrientIcons.Any()) { var icons = staticExtRepo.GetNutrientIcons(); _context.NutrientIcons.AddRange(icons); } if (!_context.StaticDataVersions.Any()) { var version = staticExtRepo.GetStaticDataVersion(); _context.StaticDataVersions.Add(new StaticDataVersion { Version = version }); } if (!_context.NitrateCreditSampleDates.Any()) { var dates = staticExtRepo.GetNitrateCreditSampleDates(); _context.NitrateCreditSampleDates.AddRange(dates); } if (!_context.ManureImportedDefaults.Any()) { var defaultManure = staticExtRepo.GetManureImportedDefault(); _context.Add(defaultManure); } //MainMenu //SubMenu if (!_context.MainMenus.Any()) { var mainMenus = staticExtRepo.GetMainMenus(); var subMenus = staticExtRepo.GetSubMenus(); foreach (var mainMenu in mainMenus) { var subMenu = subMenus.Where(s => s.MainMenuId == mainMenu.Id).ToList(); if (subMenu.Any()) { mainMenu.SubMenus.AddRange(subMenu); } } _context.MainMenus.AddRange(mainMenus); } if (!_context.LiquidMaterialsConversionFactors.Any()) { var conversions = staticExtRepo.GetLiquidMaterialsConversionFactors(); _context.LiquidMaterialsConversionFactors.AddRange(conversions); } if (!_context.SolidMaterialsConversionFactors.Any()) { var conversions = staticExtRepo.GetSolidMaterialsConversionFactors(); _context.SolidMaterialsConversionFactors.AddRange(conversions); } if (!_context.LiquidMaterialApplicationUsGallonsPerAcreRateConversions.Any()) { var conversions = staticExtRepo.GetLiquidMaterialApplicationUSGallonsPerAcreRateConversion(); _context.LiquidMaterialApplicationUsGallonsPerAcreRateConversions.AddRange(conversions); } if (!_context.SolidMaterialApplicationTonPerAcreRateConversions.Any()) { var conversions = staticExtRepo.GetSolidMaterialApplicationTonPerAcreRateConversions(); _context.SolidMaterialApplicationTonPerAcreRateConversions.AddRange(conversions); } _context.StaticDataVersions.Update(currentVersion); _context.SaveChanges(); AppliedMigrationsSeedData(); }
public void Seed() { var loadSeedDataEnv = Environment.GetEnvironmentVariable("LOAD_SEED_DATA"); var loadSeedData = (!string.IsNullOrEmpty(loadSeedDataEnv) && loadSeedDataEnv.ToLower() == "true") || _options.Value.LoadSeedData; var refreshDatabaseEnv = Environment.GetEnvironmentVariable("REFRESH_DATABASE"); var refreshDatabase = (!string.IsNullOrEmpty(refreshDatabaseEnv) && refreshDatabaseEnv.ToLower() == "true") || _options.Value.RefreshDatabase; var loadSeedConfigDataAsNewVersion = false; var expectedSeedDataVersionEnv = Environment.GetEnvironmentVariable("EXPECTED_SEED_DATA_VERSION"); var expectedSeedDataVersion = _options.Value.ExpectedSeedDataVersion; if (!string.IsNullOrEmpty(expectedSeedDataVersionEnv) && int.TryParse(expectedSeedDataVersionEnv, out int parsedVersion)) { expectedSeedDataVersion = parsedVersion; } if (refreshDatabase) { _context.Database.ExecuteSqlRaw(@"TRUNCATE TABLE ""Browsers"", ""Depths"", ""ExternalLinks"", ""SubMenu"", ""MainMenus"", ""Journey"", ""MiniAppLabels"", ""MiniApps"", ""NutrientIcons"", ""UserPrompts"";"); loadSeedConfigDataAsNewVersion = true; } if (!_context.Browsers.Any()) { var browsers = SeedDataLoader.GetSeedJsonData <List <Browser> >(Constants.SeedDataFiles.Browsers); _context.Browsers.AddRange(browsers); _context.SaveChanges(); } if (!_context.Depths.Any()) { var depths = SeedDataLoader.GetSeedJsonData <List <Depth> >(Constants.SeedDataFiles.Depths); _context.Depths.AddRange(depths); _context.SaveChanges(); } if (!_context.ExternalLinks.Any()) { var links = SeedDataLoader.GetSeedJsonData <List <ExternalLink> >(Constants.SeedDataFiles.ExternalLinks); _context.ExternalLinks.AddRange(links); _context.SaveChanges(); } if (!_context.Locations.Any()) { var locations = SeedDataLoader.GetSeedJsonData <List <Location> >(Constants.SeedDataFiles.Location); _context.Locations.AddRange(locations); _context.SaveChanges(); } if (!_context.Journeys.Any()) { var journeys = SeedDataLoader.GetSeedJsonData <List <Journey> >(Constants.SeedDataFiles.Journey); _context.Journeys.AddRange(journeys); _context.SaveChanges(); } //if (!_context.ManageVersionUsers.Any()) //{ // var locations = SeedDataLoader.GetSeedJsonData<>(Constants.SeedDataFiles.ManageVersionUsers); // _context.Locations.AddRange(locations); // _context.SaveChanges(); //} if (!_context.MiniApps.Any()) { var miniapps = SeedDataLoader.GetSeedJsonData <List <MiniApp> >(Constants.SeedDataFiles.MiniApps); _context.MiniApps.AddRange(miniapps); _context.SaveChanges(); } // This will be an example to replace the other seedings for the non static data tables, // so adding new values to the associated json file will be inserted into the table var miniapplabels = SeedDataLoader.GetSeedJsonData <List <MiniAppLabel> >(Constants.SeedDataFiles.MiniAppLabels); if (_context.MiniAppLabels.Count() != miniapplabels.Count()) { var newData = miniapplabels.Where(seedData => !_context.MiniAppLabels.Select(mal => mal.Id).Contains(seedData.Id)).ToList(); _context.MiniAppLabels.AddRange(newData); _context.SaveChanges(); } if (!_context.NutrientIcons.Any()) { var icons = SeedDataLoader.GetSeedJsonData <List <NutrientIcon> >(Constants.SeedDataFiles.NutrientIcons); _context.NutrientIcons.AddRange(icons); _context.SaveChanges(); } if (!_context.UserPrompts.Any()) { var prompts = SeedDataLoader.GetSeedJsonData <List <UserPrompt> >(Constants.SeedDataFiles.UserPrompts); _context.UserPrompts.AddRange(prompts); _context.SaveChanges(); } var seedStaticDataVersion = SeedDataLoader.GetSeedJsonData <StaticDataVersion>(Constants.SeedDataFiles.StaticDataVersion); if (!_context.StaticDataVersions.Any() || seedStaticDataVersion.Id > _sd.GetStaticDataVersionId() || (loadSeedData && expectedSeedDataVersion > _sd.GetStaticDataVersionId()) || loadSeedConfigDataAsNewVersion) { _sd.LoadConfigurations(seedStaticDataVersion); } }