Question

I have a wizard step in which a user fills in fields. I then use json to save the values into my database for each wizard step. However, in my repository I have my savechanges(). But it wont save the changes, instead it throws an error:

Entities in 'NKImodeledmxContainer.SelectedQuestion' participate in the 'QuestionSelectedQuestion' relationship. 0 related 'Question' were found. 1 'Question' is expected.

Anyone know how to get rid of the error? Do I have to get the ID from Question and save it aswell to my database or can I change something in EF so the error message is not getting thrown?

This is my post in my controller:

        [HttpPost]
    public JsonResult AnswerForm(int id, SelectedQuestionViewModel model)
    {
        bool result = false;
        var goalCardQuestionAnswer = new GoalCardQuestionAnswer();
        goalCardQuestionAnswer.SelectedQuestion = new SelectedQuestion();

        goalCardQuestionAnswer.SelectedQuestion.Id = model.QuestionID;
        goalCardQuestionAnswer.Comment = model.Comment;
        goalCardQuestionAnswer.Grade = model.Grade;

            if (goalCardQuestionAnswer.Grade != null)
            {

                    answerNKIRepository.SaveQuestionAnswer(goalCardQuestionAnswer);
                    answerNKIRepository.Save();
                    result = true;
                    return Json(result);                   
            }

       answerNKIRepository.SaveQuestionAnswer(goalCardQuestionAnswer);
       answerNKIRepository.Save();

        return Json(result);
    }

My Repository

    public class AnswerNKIRepository
{
    private readonly NKImodeledmxContainer db = new NKImodeledmxContainer();

    public List<SelectedQuestion> GetAllSelectedQuestionsByGoalCardId(int goalCardId)
    {
        return db.SelectedQuestion.Where(question => question.GoalCard.Id == goalCardId).ToList();
    }

    public void SaveQuestionAnswer(GoalCardQuestionAnswer goalCardQuestionAnswer)
    {
        db.GoalCardQuestionAnswer.AddObject(goalCardQuestionAnswer);
    }

    public void Save()
    {
        db.SaveChanges();
    }
}

This is my ViewModel:

 public class SelectedQuestionViewModel
{

    public int? Grade { get; set; }
    public string Comment { get; set; }
    public string SelectedQuestionText { get; set; }
    public int QuestionID { get; set; }
}

This is my database model:

enter image description here

Was it helpful?

Solution

The exception complains that SelectedQuestion.Question is a required navigation property but you don't set this property in your code. Try to load the question by Id from the repository and set it to the SelectedQuestion.Question reference: Replace this line ...

goalCardQuestionAnswer.SelectedQuestion.Id = model.QuestionID;

...by...

goalCardQuestionAnswer.SelectedQuestion.Question =
    answerNKIRepository.GetQuestionById(model.QuestionID);

And in your repository add the method:

public Question GetQuestionById(int id)
{
    return db.Question.Single(q => q.Id == id);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top