例外:违反了多重约束

时间:2015-02-16 01:20:50

标签: asp.net-mvc entity-framework

将Question和PossibleAnswer的关系从多对多更改为一对多后,我得到一个例外:“对数据库的更改已成功提交,但更新对象上下文时发生错误.ObjectContext可能处于不一致状态。内部异常消息:违反了多重约束。关系'WebUI.Models.PossibleAnswer_Question'的角色'PossibleAnswer_Question_Source'具有多重性1或0..1。“这是什么意思?

这是我的模特:

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; }
    }

查看问题的模型(我知道ToQuestion应该在控制器中,我稍后会重新安排它):

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;
        }
    }

以下是我创建新问题的帖子方法:

[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);
}

订单 question.PossibleAnswers.Add(new PossibleAnswer { Text = possibleAnswer }); 导致异常,因为在我将它们添加到问题之前我没有在数据库中保存可能的答案...但是如何在不使用DbContext的情况下将它们添加到数据库中(因为在转换的方法中使用DbContext不是一个好习惯查看模型到模型)?

1 个答案:

答案 0 :(得分:1)

错误即将发生,因为PossibleAnswers未被标记为已添加的实体。

请更新您的QuestionVM,如下所示:

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;
    }
}

然后更新您的代码,如下所示。

    if (ModelState.IsValid)
    {
        Question question = questionVM.ToQuestion();
        context.Questions.Add(question);
        context.SaveChanges();
        question.PossibleAnswers.AddRange(questionVM.GetPosibleAnswers());
        context.SaveChanges();
        return RedirectToAction("Questions");
     }