Reputation: 154
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();
}
}
Upvotes: 4
Views: 3270
Reputation: 150108
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
.
Upvotes: 6
Reputation: 5606
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();
Upvotes: 1
Reputation: 34687
You are never inserting the new item into the table:
ef.AnswerComments.Insert(ac);
ef.SaveChanges();
Upvotes: 0