public void TestAddQuestTemplate() { quest_template template = new quest_template { created_date = DateTime.Now, creator = user, title = "QuestTitle", description = "Description", icon = "" }; _db.quest_template.Add(template); DatabaseHelper.TrySaveChanges(ref _db); }
/// <summary> /// Imports the quest templates from V2 /// </summary> /// <param name="questTemplateTable">File w/ quest template data</param> /// <param name="questAchievementStepTable">Achievement steps data</param> /// <param name="delimiter">Delimiter between data</param> /// <param name="work">The DB access</param> private void ImportQuestTemplates( HttpPostedFileBase questTemplateTable, HttpPostedFileBase questAchievementStepTable, String delimiter, UnitOfWork work) { // Grab the data List<Dictionary<String, String>> questData = GetDataFromFile(questTemplateTable, delimiter); if (questData == null) { ModelState.AddModelError("", "Error with Quest Template table. Check Debug Output"); return; } List<Dictionary<String, String>> stepData = GetDataFromFile(questAchievementStepTable, delimiter); if (stepData == null) { ModelState.AddModelError("", "Error with Quest Step table. Check Debug Output"); return; } // Loop through quests try { work.EntityContext.Configuration.AutoDetectChangesEnabled = false; foreach (Dictionary<String, String> row in questData) { // Get the creator ImportedUser creator = GetImportedUserByOldID(row["creatorID"]); if (creator == null || creator.NewID == 0) continue; ImportedUser modifiedBy = GetImportedUserByOldID(row["last_modified_by"]); if (modifiedBy == null || modifiedBy.NewID == 0) continue; int oldID = int.Parse(row["questID"]); int threshold = int.Parse(row["quest_threshhold"]); quest_template quest = new quest_template() { created_date = DateTime.Parse(row["date_created"]), creator_id = creator.NewID, description = row["description"], featured = Boolean.Parse(row["is_featured"]), icon = row["icon"], icon_file_name = "", last_modified_by_id = modifiedBy.NewID, last_modified_date = DateTime.Parse(row["date_modified"]), posted_date = DateTime.Parse(row["date_posted"]), retire_date = null, state = (int)JPPConstants.AchievementQuestStates.Inactive, threshold = threshold <= 0 ? (int?)null : threshold, title = row["title"], user_generated = false, keywords = "" }; ImportedEarnable impQuest = new ImportedEarnable() { OldID = oldID, UniqueData = quest.title }; work.EntityContext.quest_template.Add(quest); _questMap.Add(impQuest.OldID, impQuest); } } finally { work.EntityContext.Configuration.AutoDetectChangesEnabled = true; } work.SaveChanges(); foreach (ImportedEarnable impQuest in _questMap.Values) { impQuest.NewID = (from q in work.EntityContext.quest_template where q.title == impQuest.UniqueData select q.id).FirstOrDefault(); } // Achievement steps try { work.EntityContext.Configuration.AutoDetectChangesEnabled = false; foreach (Dictionary<String, String> row in stepData) { // Get the achievement and quest ImportedEarnable quest = GetImportedEarnableByOldID(_questMap, row["questID"]); if (quest == null || quest.NewID == 0) continue; ImportedEarnable achieve = GetImportedEarnableByOldID(_achievementMap, row["achievementID"]); if (achieve == null || achieve.NewID == 0) continue; quest_achievement_step step = new quest_achievement_step() { achievement_id = achieve.NewID, quest_id = quest.NewID }; work.EntityContext.quest_achievement_step.Add(step); } } finally { work.EntityContext.Configuration.AutoDetectChangesEnabled = true; } work.SaveChanges(); }