2

After changing relationship of Question and PossibleAnswer from Many-to-many to One-to-many I got an exception: "The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Multiplicity constraint violated. The role 'PossibleAnswer_Question_Source' of the relationship 'WebUI.Models.PossibleAnswer_Question' has multiplicity 1 or 0..1." What does it mean?

Here is my model:

public class Question { public int ID { get; set; } public string Text { get; set; } public bool IsAssociatedWithProfessor { get; set; } public bool IsAssociatedWithAssistant { get; set; } public virtual ICollection<PossibleAnswer> PossibleAnswers { get; set; } public virtual ICollection<Results> Results { get; set; } } public class PossibleAnswer { public int ID { get; set; } public string Text { get; set; } public virtual Question Question { get; set; } } 

And view model for Question (I know that ToQuestion should be in controller, I will rearrange it later):

public class QuestionVM { public QuestionVM() { } public QuestionVM(Question question) : this() { ID = question.ID; Text = question.Text; IsAssociatedWithProfessor = question.IsAssociatedWithProfessor; IsAssociatedWithAssistant = question.IsAssociatedWithAssistant; } public int? ID { get; set; } public string Text { get; set; } public bool IsAssociatedWithProfessor { get; set; } public bool IsAssociatedWithAssistant { get; set; } private IEnumerable<string> _possibleAnswers; public IEnumerable<string> PossibleAnswers { get { return _possibleAnswers ?? new List<string>(){"", "", "", "", ""}; } set { _possibleAnswers = value; } } public Question ToQuestion() { Question question = new Question { Text = this.Text, IsAssociatedWithProfessor = this.IsAssociatedWithProfessor, IsAssociatedWithAssistant = this.IsAssociatedWithAssistant, PossibleAnswers = new List<PossibleAnswer>() }; //ID will be null if creating new question if(ID != null) { question.ID = (int) ID; } foreach (string possibleAnswer in this.PossibleAnswers) { if (!String.IsNullOrEmpty(possibleAnswer)) { question.PossibleAnswers.Add(new PossibleAnswer { Text = possibleAnswer }); } } return question; } } 

Here is my post method for creating new question:

[HttpPost] [ValidateAntiForgeryToken] public ActionResult AddQuestion(QuestionVM questionVM) { try { if (ModelState.IsValid) { Question question = questionVM.ToQuestion(); context.Questions.Add(question); context.SaveChanges(); return RedirectToAction("Questions"); } } catch (DataException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Trenutno nije moguće snimiti promjene, pokušajte ponovo."); } return View(questionVM); } 

Line:question.PossibleAnswers.Add(new PossibleAnswer { Text = possibleAnswer }); causes the exception since I didn't save possible answers in database before I am adding them to question... But how can I add them to database without use of DbContext (since it is not a good practice to use DbContext in method which converts view model to model)?

4
  • Post some line that causes the exception and also indicate that this line resulted in exception. Commented Feb 16, 2015 at 10:03
  • 1
    All seems to be good. What is implementation of ToQuestion method in QuestionVM? Commented Feb 16, 2015 at 10:14
  • I put that in question too. Commented Feb 16, 2015 at 10:20
  • See my answer below. Commented Feb 16, 2015 at 10:50

1 Answer 1

1

Error is coming because PossibleAnswers are not being marked as added entity.

So update your QuestionVM like below:

public class QuestionVM { public QuestionVM() { } public QuestionVM(Question question) : this() { ID = question.ID; Text = question.Text; IsAssociatedWithProfessor = question.IsAssociatedWithProfessor; IsAssociatedWithAssistant = question.IsAssociatedWithAssistant; } public int? ID { get; set; } public string Text { get; set; } public bool IsAssociatedWithProfessor { get; set; } public bool IsAssociatedWithAssistant { get; set; } private IEnumerable<string> _possibleAnswers; public IEnumerable<string> PossibleAnswers { get { return _possibleAnswers ?? new List<string>(){"", "", "", "", ""}; } set { _possibleAnswers = value; } } public Question ToQuestion() { Question question = new Question { Text = this.Text, IsAssociatedWithProfessor = this.IsAssociatedWithProfessor, IsAssociatedWithAssistant = this.IsAssociatedWithAssistant, PossibleAnswers = new List<PossibleAnswer>() }; //ID will be null if creating new question if(ID != null) { question.ID = (int) ID; } return question; } public List<PossibleAnswer> GetPosibleAnswers() { var listOfPossibleAnswers = new List<PossibleAnswer>(); foreach (string possibleAnswer in this.PossibleAnswers) { if (!String.IsNullOrEmpty(possibleAnswer)) { listOfPossibleAnswers.Add(new PossibleAnswer { Text = possibleAnswer }); } } return listOfPossibleAnswers; } } 

and then update your code like below.

 if (ModelState.IsValid) { Question question = questionVM.ToQuestion(); context.Questions.Add(question); context.SaveChanges(); question.PossibleAnswers.AddRange(questionVM.GetPosibleAnswers()); context.SaveChanges(); return RedirectToAction("Questions"); } 
Sign up to request clarification or add additional context in comments.

4 Comments

@bambiinela Error is coming because PossibleAnswers are not being marked as added entity. Please remove that line question.PossibleAnswers.Add(new PossibleAnswer { Text = possibleAnswer, Question=question }); and then try saving.
You are right, that line causes problem. But how else can I add possible answers to my database and to question too (but without using my DbContext, since it is not good practice to use DbContext in method that convert view models to models and vice versa) ?
@bambiinela Save the question first without the PossibleAnswer and then update question with adding possibleAnswers to the collection of question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.