0

需要这方面的帮助。我需要在控制器操作中将会话中的一些数据保存到数据库。但是我在 answer.Add(answer); 上得到“一个实体对象不能被 C# 中 IEntityChangeTracker 的多个实例引用”错误

有人可以帮我吗?

Questionare questionare = unitOfWork.QuestionareRepository.GetByID(id);



            SADEntitiesContext db = new SADEntitiesContext();

            foreach (Question question in questionare.Questions)
            {
                //check if there are data in Session and save it
                _question = "question"+question.QuestionID.ToString();


                if (Session[_question] != null)
                {

                    var answer = new Answers();
                    if (TryUpdateModel(answer))
                    {

                    questionanswer = (QuestionAnswerData)Session[_question];
                    int qID = Int16.Parse(questionanswer.QuestionID);
                    var answertoupdate = answer.GetAnswer(qID, questionanswer.UserID, questionanswer.EmployeID);


                    //db.Answers.Remove(answertoupdate);


                    answer.UserName = questionanswer.UserID;
                    answer.Answer = db.AnswerChoices.Find(Int16.Parse(questionanswer.AnswerID));
                    answer.AnsweredAt = DateTime.Now;
                    answer.locked = false;
                    answer.Question = question;
                    answer.Questionare = questionare;
                    if (questionanswer.EmployeID != null)
                    {
                        answer.AnswerAboutUser = questionanswer.EmployeID;
                    }
                    if (answertoupdate != null)
                    {
                        answertoupdate = answer;

                        ok = (answertoupdate.Save() > 0);
                    }

                    else
                    {
                        answer.Add(answer);


                        ok = (answer.Save() > 0);
                    }
                    }
                }

答案类公共类答案{

    SADEntitiesContext db = new SADEntitiesContext();

    public int AnswersId { get; set; }

    //[Display(Name = "DataResposta", ResourceType = typeof(Resources))]
    public DateTime AnsweredAt { get; set; }

    //[Display(Name = "bloqueado", ResourceType = typeof(Resources))]
    public bool locked { get; set; }

   // [Display(Name = "UserName", ResourceType = typeof(Resources))]
    public string UserName { get; set; }

    //[Display(Name = "AnswersAboutUser", ResourceType = typeof(Resources))]
    public string AnswerAboutUser { get; set; } 

    //[Display(Name = "Resposta", ResourceType = typeof(Resources))]
    public virtual AnswerChoices Answer { get; set; } 

    //[Display(Name = "Questionare", ResourceType = typeof(Resources))]
    public virtual Questionare Questionare { get; set; }

    //[Display(Name = "QuestionID", ResourceType = typeof(Resources))]
    public virtual Question Question { get; set; }


    //
    // Persistence 

    public int Save()
    {
        return db.SaveChanges();
    }


    public Answers GetAnswer(int questionID, string employeID, string userID)
    {
        return db.Answers 
            .Where(e => e.UserName == userID
                                             && e.Question.QuestionID == questionID
                                             && e.AnswerAboutUser == employeID)

                                         .FirstOrDefault();

    }

    public Answers GetAnswer(int id)
    {
        return db.Answers.SingleOrDefault(d => d.AnswersId == id);
    }

    //
    // Insert/Delete Methods

    public void Add(Answers _answer)
    {
        db.Answers.Add(_answer);
        db.SaveChanges();
    }
}

}

4

1 回答 1

0

您的问题是您正在从一个上下文中调用答案:

answer.Answer = db.AnswerChoices.Find(Int16.Parse(questionanswer.AnswerID));

然后尝试在您的 Answer 类中使用不同的方法保存它:

  SADEntitiesContext db = new SADEntitiesContext();

解决问题的最快方法是能够在您的 Answer 类中设置上下文,这样您就不会跳转上下文,或者您可以在第一次从数据库中提取答案后分离答案,然后在保存中重新附加它.

但是,我认为你有一个更根本的问题。您确定让一个类能够创建自己的上下文并保存自己是个好主意吗?你到处都会遇到这样的问题。

于 2012-09-21T00:38:12.680 回答