4

Using sql server profiler I can see that the initial query for AnswerComment is going to the db but when I get to the ef.SaveChanges() nothing is getting to the db. I'm using sqlexpress 2008 R2.

using (TPRDEntities ef = new TPRDEntities())
{
    var ac = ef.AnswerComments.Where(a => a.AnswerCommentID == answercomment.AnswerCommentID).FirstOrDefault();

    if (ac == null)
    {
        ac = new AnswerComment();
        ac.AnswerID = answercomment.AnswerID;
        ac.DisplayText = answercomment.DisplayText;
        ac.InsertDate = answercomment.InsertDate;
        ac.InsertUser = "save test user";
        ef.SaveChanges();
    }
}

3 Answers 3

6

The new instance you create

ac = new AnswerComment();  

is not known to EF. It is a brand-new object instance that EF has not seen before.

You will have to add it to ef.AnswerComments

ef.AnswerComments.Insert(ac);

Also, ensure that ChangeTracking is active for ef.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow a downvote to an accepted answer one year later with no comment and no better answer.
The downvote is from me, i did a mistake... I cannot undo it until the answer is edited, sorry...
@schglurps: I have edited the answer. Thank you for correcting the accidental downvote.
1

I think you miss to add the AnswerComment to the Context, you've to do:

ac = new AnswerComment();
ac.AnswerID = answercomment.AnswerID;
ac.DisplayText = answercomment.DisplayText;
ac.InsertDate = answercomment.InsertDate;
ac.InsertUser = "save test user";

ef.AnswerComments.Add(ac);
ef.SaveChanges();

Comments

0

You are never inserting the new item into the table:

ef.AnswerComments.Insert(ac);
ef.SaveChanges();

Comments

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.