1

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

1
  • You are not showing what is happening in your repository which is probably most important part of your question. Commented Apr 24, 2012 at 12:57

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

10 Comments

With that code i get following error message: "Object reference not set to an instance of an object"
At the line you suggested I should replace: "goalCardQuestionAnswer.SelectedQuestion.Question = answerNKIRepository.GetQuestionById(model.QuestionID);"
Is there anyway to not get Question enitity involved in my controller? I would prefer just to use SelectedQuestion and GoalCardQuestionAnswer to be used. I need to fill the foreign key in my GoalCardQuestionAnswer entity by SelectedQuestion ID
@ps__: Can you check in the debugger if one of the objects goalCardQuestionAnswer or goalCardQuestionAnswer.SelectedQuestion or answerNKIRepository or model is null? I can't imagine the first two are null because you create them with new in the lines before.
Your code creates a new SelectedQuestion. Don't you want this? Does the SelectedQuestion already exist in the DB?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.