Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:58:10+00:00 2026-06-11T15:58:10+00:00

Need help with this please. I need to save some data in session to

  • 0

Need help with this please.
I need to save some data in session to DB on controller action. But i get the “An entity object cannot be referenced by multiple instances of IEntityChangeTracker in C#” error on
answer.Add(answer);

can anybody help me with this?

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

answers class
public class Answers
{

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:58:11+00:00Added an answer on June 11, 2026 at 3:58 pm

    Your problem is you’re calling an answer from one context:

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

    and then attempting to save it using a different in your Answer class:

      SADEntitiesContext db = new SADEntitiesContext();
    

    The quickest solution to your problem would be to be able to set the context in your Answer class, so that your not jumping contexts, or you could Detach the answer after pulling it from the db the first time, and then reattach it in the save.

    However, I think you have a more fundamental problem. Are you sure it’s a good idea to have a class be able to create it’s own context, and save itself? You’ll have problems like this all over the place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help to get this right, please. I have created an iPad
I need some help with this, please ... If I save a transaction status
Please i need someone to help convert this query entity(c#) form. select * from
Please i need help, this code below works fine on my localhost, php5.3+ but
I need some help with a LINQ query in VB.Net, please. I have this
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Please can anyone help me? I search some example how can i get information
I need to obtain some data from QNetworkReply to parse later, but every time
I really need help this time - Joomla 2.5 and latest K2. I'm using
i need help with this. I'am using JQuery's Dynamic Form plug-in, it duplicates my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.