Inheritance: INotifyPropertyChanging, INotifyPropertyChanged
 /// <summary>
 /// Updates the given <see cref="Question"/> <paramref name="instance"/>'s <see cref="Question.RequiredMessage"/>.
 /// </summary>
 /// <param name="instance">The <see cref="Question"/> instance to update.</param>
 private static void UpdateQuestionRequiredMessage(Question instance)
 {
     instance.RequiredMessage = string.Format(
             CultureInfo.CurrentCulture,
             Localization.GetString("Required Question.Format", SurveyRepository.SharedResourceFile),
             instance.Text);
 }
        /// <summary>
        /// Called when a new <see cref="Question"/> is inserted.
        /// </summary>
        /// <param name="instance">The new <see cref="Question"/> instance.</param>
partial         void InsertQuestion(Question instance)
        {
            instance.CreationDate = DateTime.Now;
            instance.RevisionDate = DateTime.Now;

            UpdateQuestionRequiredMessage(instance);

            this.ExecuteDynamicInsert(instance);
        }
        public object UpdateQuestion(int surveyId, Question question)
        {
            var surveyRepository = new SurveyRepository();
            var survey = surveyRepository.LoadSurvey(surveyId);
            if (!this.CanEditModule(survey.ModuleId))
            {
                this.DenyAccess();
            }

            Question questionToUpdate;
            if (question.QuestionId > 0)
            {
                questionToUpdate =
                    survey.Sections.First().Questions.Where(q => q.QuestionId == question.QuestionId).Single();
                questionToUpdate.RevisingUser = question.RevisingUser;
            }
            else
            {
                questionToUpdate = surveyRepository.CreateQuestion(question.RevisingUser);
                survey.Sections.First().Questions.Add(questionToUpdate);
            }

            questionToUpdate.Text = question.Text;
            questionToUpdate.IsRequired = question.IsRequired;
            questionToUpdate.RelativeOrder = question.RelativeOrder;
            questionToUpdate.ControlType = question.ControlType;

            var answersToDelete = from answer in questionToUpdate.Answers
                                  where !question.Answers.Any(a => a.AnswerId == answer.AnswerId)
                                  select answer;
            surveyRepository.DeleteAnswers(answersToDelete, true);

            int answerOrder = 0;
            foreach (var answer in question.Answers)
            {
                Answer answerToUpdate;
                if (answer.AnswerId > 0)
                {
                    var lambdaAnswer = answer;
                    answerToUpdate = questionToUpdate.Answers.Where(a => a.AnswerId == lambdaAnswer.AnswerId).Single();
                    answerToUpdate.RevisingUser = question.RevisingUser;
                }
                else
                {
                    answerToUpdate = surveyRepository.CreateAnswer(question.RevisingUser);
                    questionToUpdate.Answers.Add(answerToUpdate);
                }

                answerToUpdate.Text = answer.Text;
                answerToUpdate.RelativeOrder = ++answerOrder;
            }

            surveyRepository.SubmitChanges();

            return
                new
                    {
                        questionToUpdate.QuestionId,
                        questionToUpdate.ControlType,
                        questionToUpdate.RelativeOrder,
                        questionToUpdate.Text,
                        questionToUpdate.IsRequired,
                        Answers =
                            questionToUpdate.Answers.OrderBy(a => a.RelativeOrder).Select(
                                a => new { a.AnswerId, a.RelativeOrder, a.Text })
                    };
        }
		private void detach_Questions(Question entity)
		{
			this.SendPropertyChanging();
			entity.Section = null;
		}
		private void attach_Questions(Question entity)
		{
			this.SendPropertyChanging();
			entity.Section = this;
		}
 partial void DeleteQuestion(Question instance);
 partial void UpdateQuestion(Question instance);
 partial void InsertQuestion(Question instance);
        /// <summary>
        /// Creates a new <see cref="Question"/> instance, to be persisted when <see cref="SubmitChanges"/> is called.
        /// </summary>
        /// <param name="userId">The ID of the user creating the instance.</param>
        /// <returns>The new <see cref="Question"/> instance</returns>
        public Question CreateQuestion(int userId)
        {
            var question = new Question(userId);

            this.Context.Questions.InsertOnSubmit(question);

            return question;
        }