I am simply creating a new object that has one to on relationship. A comment to a User relation. When I try to retrieve the new object I get the error Cannot access a disposed object. I am not sure what is going on. Thanks for any help or suggestions.
using (var db = new LinqEntityDataContext())
{
var comment = new Comment();
comment.CommentBy = GlobalVariables.User.ID;
comment.OutPutMessage = commentText.Trim();
comment.PhotoID = int.Parse(pictureID);
comment.CommentDate = DateTime.Now;
db.Comments.InsertOnSubmit(comment);
db.SubmitChanges();
return comment;
}
You need to set the
return comment;after the last bracket.You are finishing the function just before the Dispose, that’s why you get the Exception
Something like this.