public static async void Initialize(DatasetsContext dsContext, QuestionnairesContext qContext, UserManager <IdentityUser> userManager) { dsContext.Database.EnsureCreated(); if (dsContext.Datasets.Any()) { return; //If this happens the DB already exists and has been seeded with test data } var deelnemers = await userManager.GetUsersInRoleAsync("Deelnemer"); string participant = "Dummy"; if (deelnemers.Count > 0) { participant = deelnemers[0].Id; } dsContext.Datasets.Add(new Dataset { ResearchID = 1, ResearchPhaseID = 1, QuestionnaireID = 1, ParticipantID = participant, SubmissionDate = DateTime.Now }); dsContext.SaveChanges(); var questionnaire = qContext.Questionnaires .FirstOrDefault(); var responses = new List <Response>(); foreach (var section in questionnaire.QuestionnaireSections) { foreach (var item in section.QuestionnaireItems) { new Response { ResponseID = responses.Count + 1, DatasetID = 1, ResponseType = item.ItemType, Data = item.QuestionnaireItemID.ToString() }; } } foreach (Response r in responses) { dsContext.Responses.Add(r); } dsContext.SaveChanges(); }
public static void Initialize(QuestionnairesContext context) { context.Database.EnsureCreated(); if (context.Questionnaires.Any()) { return; //If this happens the DB already exists and has been seeded with test data } var questionnaires = new Questionnaire[] { new Questionnaire { Title = "Example questionnaire", IntroText = "A questionnaire on an example topic" } }; foreach (Questionnaire q in questionnaires) { context.Questionnaires.Add(q); } context.SaveChanges(); var sections = new QuestionnaireSection[] { new QuestionnaireSection { QuestionnaireID = 1, Title = "Section 1", IntroText = "Intro 1" }, new QuestionnaireSection { QuestionnaireID = 1, Title = "Section 2", IntroText = "Intro 2" } }; foreach (QuestionnaireSection s in sections) { context.QuestionnaireSections.Add(s); } context.SaveChanges(); var items = new QuestionnaireItem[] { new QuestionnaireItem { QuestionnaireSectionID = 1, ItemText = "Item 1", ItemType = ItemTypes.Text }, new QuestionnaireItem { QuestionnaireSectionID = 1, ItemText = "Item 2", ItemType = ItemTypes.Value }, new QuestionnaireItem { QuestionnaireSectionID = 1, ItemText = "Item 3", ItemType = ItemTypes.Likert }, new QuestionnaireItem { QuestionnaireSectionID = 2, ItemText = "Item 4", ItemType = ItemTypes.Text }, new QuestionnaireItem { QuestionnaireSectionID = 2, ItemText = "Item 5", ItemType = ItemTypes.Value }, new QuestionnaireItem { QuestionnaireSectionID = 2, ItemText = "Item 6", ItemType = ItemTypes.Likert } }; foreach (QuestionnaireItem i in items) { context.QuestionnaireItems.Add(i); } context.SaveChanges(); }