private static int SaveOrganisationsToDb(OrganisationsInput organisations, CategoriesInput categories) { var changes = 0; using (var context = new CdcsContext()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); context.Organisations.AddRange(organisations.Organisations); context.Accreditors.AddRange(organisations.Accreditors); context.Accreditations.AddRange(organisations.Accreditations); context.ContactModes.AddRange(organisations.ContactModes); context.GeographicalZones.AddRange(organisations.GeographicalZones); context.OrganisationLanguages.AddRange(organisations.OrganisationLanguages); context.OrganisationLegalStatuses.AddRange(organisations.OrganisationLegalStatuses); context.Languages.AddRange(organisations.Languages); context.Sectors.AddRange(categories.Sectors); context.Topics.AddRange(categories.Topics); context.Categories.AddRange(categories.Categories); context.SectorCategoriesHierarchies.AddRange(categories.SectorCategoriesHierarchies); changes = context.SaveChanges(); } return(changes); }
private static void Check_ForeignKeys(OrganisationsInput organisations) { var missingName = new BilingualItem("Missing", "Missing"); //TODO: add unknown missing foreign keys // Accreditations List <int> idAcs = organisations.Accreditors.Select(x => x.Id).ToList(); List <Accreditation> missingAccreditors = organisations.Accreditations.FindAll(x => x.AccreditorId == 0 || !idAcs.Contains(x.AccreditorId)); var missingAccreditor = new Accreditor { Id = 999999, Name = missingName }; missingAccreditors.ForEach(x => x.Accreditor = missingAccreditor); // organisations.Accreditations.RemoveAll(x => x.AccreditorId == 0 || !idAcs.Contains(x.AccreditorId)); // Organisations List <int> idLss = organisations.OrganisationLegalStatuses.Select(x => x.Id).ToList(); var missingLegalStatus = new OrganisationLegalStatus { Id = 999999, Name = missingName }; List <Organisation> missingStatuses = organisations.Organisations.FindAll(x => x.LegalStatusId == 0 || !idLss.Contains(x.LegalStatusId)); missingStatuses.ForEach(x => x.LegalStatus = missingLegalStatus); // organisations.Organisations.RemoveAll(x => x.LegalStatusId == 0 || !idLss.Contains(x.LegalStatusId)); }