public void AppliedMigrationsSeedData() { //Updates if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("1_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("1_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("2_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("2_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("3_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("3_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("4_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("4_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { var updated = _context.UserPrompts.Single(up => up.Id == newUserPrompt.Id); _mapper.Map(newUserPrompt, updated); _context.UserPrompts.Update(updated); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("11_Breed", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <Breed> >("11_Breed"); var currentVersion = _sd.GetCurrentStaticDataVersion(); foreach (var newBreed in migrationSeedData.Data) { newBreed.SetVersion(currentVersion); if (!_context.Breed.Any(up => up.Id == newBreed.Id)) { _context.Breed.Add(newBreed); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.StaticDataVersions.Update(currentVersion); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("14_AnimalSubTypes", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <AnimalSubType> >("14_AnimalSubTypes"); var currentVersion = _sd.GetCurrentStaticDataVersion(); foreach (var newSubType in migrationSeedData.Data) { if (_context.AnimalSubType.Any(up => up.Id == newSubType.Id && up.StaticDataVersionId == currentVersion.Id)) { var updated = _context.AnimalSubType.Single(up => up.Id == newSubType.Id && up.StaticDataVersionId == currentVersion.Id); _mapper.Map(newSubType, updated); _context.AnimalSubType.Update(updated); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("16_LiquidSolidSeparationDefault", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <LiquidSolidSeparationDefault> >("16_LiquidSolidSeparationDefault"); var currentVersion = _sd.GetCurrentStaticDataVersion(); foreach (var liquidSolidSeparationDefault in migrationSeedData.Data) { liquidSolidSeparationDefault.SetVersion(currentVersion); if (!_context.LiquidSolidSeparationDefaults.Any(up => up.Id == liquidSolidSeparationDefault.Id)) { _context.LiquidSolidSeparationDefaults.Add(liquidSolidSeparationDefault); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.StaticDataVersions.Update(currentVersion); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("17_SubRegions", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <SubRegion> >("17_SubRegions"); var currentVersion = _context.StaticDataVersions.Single(v => v.Id == _sd.GetCurrentStaticDataVersion().Id); foreach (var newSubRegion in migrationSeedData.Data) { newSubRegion.SetVersion(currentVersion); newSubRegion.Region = _sd.GetRegion(newSubRegion.RegionId); if (!_context.SubRegion.Any(up => up.Id == newSubRegion.Id)) { _context.SubRegion.Add(newSubRegion); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.StaticDataVersions.Update(currentVersion); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("19_PotassiumSoilTestRanges", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <PotassiumSoilTestRange> >("19_PotassiumSoilTestRanges"); var currentVersion = _sd.GetCurrentStaticDataVersion(); foreach (var testRange in migrationSeedData.Data) { var currentTestRange = _context.PotassiumSoilTestRanges.Single(s => s.Id == testRange.Id); currentTestRange.LowerLimit = testRange.LowerLimit; currentTestRange.UpperLimit = testRange.UpperLimit; currentTestRange.Rating = testRange.Rating; currentTestRange.SetVersion(currentVersion); } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.StaticDataVersions.Update(currentVersion); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("20_PhosphorusSoilTestRanges", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <PhosphorusSoilTestRange> >("20_PhosphorusSoilTestRanges"); var currentVersion = _sd.GetCurrentStaticDataVersion(); foreach (var testRange in migrationSeedData.Data) { var currentTestRange = _context.PhosphorusSoilTestRanges.Single(s => s.Id == testRange.Id); currentTestRange.LowerLimit = testRange.LowerLimit; currentTestRange.UpperLimit = testRange.UpperLimit; currentTestRange.Rating = testRange.Rating; currentTestRange.SetVersion(currentVersion); } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.StaticDataVersions.Update(currentVersion); _context.SaveChanges(); } //if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("18_PrevYearManureApplicationNitrogenDefaults", StringComparison.CurrentCultureIgnoreCase))) //{ // //var migrationSeedData = SeedDataLoader.GetMigrationSeedData<List<PreviousYearManureApplicationNitrogenDefault>>("18_PrevYearManureApplicationNitrogenDefaults"); // //foreach (var newPrevManureApplicationNitrogenDefault in migrationSeedData.Data) // //{ // // if (_context.PrevYearManureApplicationNitrogenDefaults.Any(up => up.Id == newPrevManureApplicationNitrogenDefault.Id)) // // { // // var updated = _context.PrevYearManureApplicationNitrogenDefaults.Single(up => up.Id == newPrevManureApplicationNitrogenDefault.Id); // // _mapper.Map(newPrevManureApplicationNitrogenDefault, updated); // // _context.PrevYearManureApplicationNitrogenDefaults.Update(updated); // // } // //} // //_context.AppliedMigrationSeedData.Add(migrationSeedData); // //_context.SaveChanges(); // var migrationSeedData = SeedDataLoader.GetMigrationSeedData<List<PreviousYearManureApplicationNitrogenDefault>>("18_PrevYearManureApplicationNitrogenDefaults"); // foreach (var newPrevManureApplicationNitrogenDefault in migrationSeedData.Data) // { // newPrevManureApplicationNitrogenDefault.Crops = _sd.GetCropsByManureApplicationHistory(newPrevManureApplicationNitrogenDefault.FieldManureApplicationHistory); // newPrevManureApplicationNitrogenDefault.PreviousManureApplicationYear = // _sd.GetPrevManureApplicationInPrevYearsByManureAppHistory(newPrevManureApplicationNitrogenDefault.FieldManureApplicationHistory); // //newPrevManureApplicationNitrogenDefault.StaticDataVersionId = currentVersion.Id; // //newPrevManureApplicationNitrogenDefault.SetVersion(currentVersion); // if (_context.PrevYearManureApplicationNitrogenDefaults.Any(up => up.Id == newPrevManureApplicationNitrogenDefault.Id)) // { // var updated = _context.PrevYearManureApplicationNitrogenDefaults.Single(up => up.Id == newPrevManureApplicationNitrogenDefault.Id); // //_mapper.Map(newPrevManureApplicationNitrogenDefault, updated); // updated.Crops = newPrevManureApplicationNitrogenDefault.Crops; // //updated.PreviousManureApplicationYear = // // newPrevManureApplicationNitrogenDefault.PreviousManureApplicationYear; // //updated.StaticDataVersionId = newPrevManureApplicationNitrogenDefault.StaticDataVersionId; // _context.PrevYearManureApplicationNitrogenDefaults.Update(updated); // } // } // _context.AppliedMigrationSeedData.Add(migrationSeedData); // _context.SaveChanges(); //} if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("21_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("21_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("22_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("22_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { var updated = _context.UserPrompts.Single(up => up.Id == newUserPrompt.Id); _mapper.Map(newUserPrompt, updated); _context.UserPrompts.Update(updated); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("5_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("5_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("6_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("6_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("24_MainMenu", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <MainMenu> >("24_MainMenu"); foreach (var menu in migrationSeedData.Data) { var mainMenu = _context.MainMenus.Single(up => up.Id == menu.Id); mainMenu.SortNumber = menu.SortNumber; _context.MainMenus.Update(mainMenu); } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("25_SubMenu", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <SubMenu> >("25_SubMenu"); foreach (var updatedMenu in migrationSeedData.Data) { var menu = _context.MainMenus .Include(m => m.SubMenus) .Single(up => up.Id == updatedMenu.MainMenuId); menu.SubMenus.Single(sb => sb.Id == updatedMenu.Id) .SortNumber = updatedMenu.SortNumber; _context.MainMenus.Update(menu); } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("8_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("8_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("23_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("23_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("26_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("26_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("27_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("27_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("27_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("27_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("9_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("9_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("10_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("10_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("12_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("12_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } if (!_context.AppliedMigrationSeedData.Any(a => a.JsonFilename.Equals("28_UserPrompts", StringComparison.CurrentCultureIgnoreCase))) { var migrationSeedData = SeedDataLoader.GetMigrationSeedData <List <UserPrompt> >("28_UserPrompts"); foreach (var newUserPrompt in migrationSeedData.Data) { if (!_context.UserPrompts.Any(up => up.Id == newUserPrompt.Id)) { _context.UserPrompts.Add(newUserPrompt); } } _context.AppliedMigrationSeedData.Add(migrationSeedData); _context.SaveChanges(); } }
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); } }