title: Entity framework savechanges error
body: <p>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:</p>
  
  <blockquote>
    <p>Entities in 'NKImodeledmxContainer.SelectedQuestion' participate in the 'QuestionSelectedQuestion' relationship. 0 related 'Question' were found. 1 'Question' is expected.</p>
  </blockquote>
  
  <p>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?</p>
  
  <p>This is my post in my controller:</p>
  
  <pre><code>        [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);
      }
  </code></pre>
  
  <p>My Repository</p>
  
  <pre><code>    public class AnswerNKIRepository
  {
      private readonly NKImodeledmxContainer db = new NKImodeledmxContainer();
  
      public List&lt;SelectedQuestion&gt; GetAllSelectedQuestionsByGoalCardId(int goalCardId)
      {
          return db.SelectedQuestion.Where(question =&gt; question.GoalCard.Id == goalCardId).ToList();
      }
  
      public void SaveQuestionAnswer(GoalCardQuestionAnswer goalCardQuestionAnswer)
      {
          db.GoalCardQuestionAnswer.AddObject(goalCardQuestionAnswer);
      }
  
      public void Save()
      {
          db.SaveChanges();
      }
  }
  </code></pre>
  
  <p>This is my ViewModel:</p>
  
  <pre><code> public class SelectedQuestionViewModel
  {
  
      public int? Grade { get; set; }
      public string Comment { get; set; }
      public string SelectedQuestionText { get; set; }
      public int QuestionID { get; set; }
  }
  </code></pre>
  
  <p>This is my database model:</p>
  
  <p><img src="https://i.stack.imgur.com/NyE5x.jpg" alt="enter image description here"></p>